You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@royale.apache.org by Harbs <ha...@gmail.com> on 2019/01/21 15:25:02 UTC

Can we do better than Promises?

Every time I have to use Promises I want to pull out my hair. It must the the most unintuitive software pattern known to man.

Async/await is somewhat better, but it’s just syntactic sugar and you end up dealing with Promises very often anyway. I also hate “magic”.

The only other pattern I am familiar with when it comes to async execution (besides nested callbacks) is Monads and FP. Functional programming is another one of my aversions. I think it goes along with my aversion to math… ;-)

I was wondering if there might be a “better” pattern for strongly typed languages such as AS3:
1. A “manager" class which has methods which accepts “Execution” instances which implement an interface.
2. The manager class would have methods for “addExection”, “addOrderedExecution” etc. The ordered execution could have the previous results automatically inserted into the subsequent execution.
3. The manager class would have a done method which takes a callback with a “Result” object.
4. It could also have an error method (or dispatch error events) in the case of errors.
5. Each “Exectution” would have an “execute” method. It could let the manager know when it was done either by a callback or by dispatching an event.

That would allow code like this:

var manager:AsyncManager = new AsyncManager();
manager.push(new ExecutionType1("foo"));
manager.push(new ExecutionType2("foo"));
manager.push(new ExecutionType3("foo"));
manager.push(new ExecutionType4("foo"));
manager.done(function(results:Array):void{
	for each(var res in results){
	trace(res);
	}
});
Manager.error(function(err:AsyncError):void{
	trace(err);
}

For ordered execution with results, it would be as simple as:
var manager:AsyncManager = new AsyncManager();
manager.pushOrdered(new ExecutionType1("foo"));
manager.pushOrdered(new ExecutionType2("foo"));
manager.pushOrdered(new ExecutionType3("foo"));
manager.pushOrdered(new ExecutionType4("foo"));

This feels much easier than promise.then, forced resolving and rejection of promises, etc.

Thoughts?
Harbs

Re: Can we do better than Promises?

Posted by Harbs <ha...@gmail.com>.
My point wasn’t the functions. It was the promises. (I always execute functions which return promises because I find that a lot easier than constructing them inline…)

Constructing and resolving promises is a pattern I find very counterintuitive.


> On Jan 21, 2019, at 7:07 PM, szogun1987 <sz...@gmail.com> wrote:
> 
> Promise.all accepts promises, not functions that returns promises.
> I can't feel anything painfull with promises, you can thread arrow function
> as a shorthand class that implements interface version.
> This class accepts contextual variables as constructor parameters, stores
> them in fields, what makes them available once "execute" method is called.
> Execute method accepts also value returned by predecessor, and value
> returned by execute is passed to successor.
> By the way many compilers work similarly behind the code.
> 
> 
> 
> --
> Sent from: http://apache-royale-development.20373.n8.nabble.com/


Re: Can we do better than Promises?

Posted by szogun1987 <sz...@gmail.com>.
Promise.all accepts promises, not functions that returns promises.
I can't feel anything painfull with promises, you can thread arrow function
as a shorthand class that implements interface version.
This class accepts contextual variables as constructor parameters, stores
them in fields, what makes them available once "execute" method is called.
Execute method accepts also value returned by predecessor, and value
returned by execute is passed to successor.
By the way many compilers work similarly behind the code.



--
Sent from: http://apache-royale-development.20373.n8.nabble.com/

Re: Can we do better than Promises?

Posted by Harbs <ha...@gmail.com>.
FWIW, a better name for “pushOrdered” would probably be “chain()”.

> On Jan 21, 2019, at 6:40 PM, Harbs <ha...@gmail.com> wrote:
> 
> Thanks for your thoughts.
> 
> You’re right that it’s similar to Promise.all, but there’s a couple of differences:
> 
> 1. With Promise.all(), you still need to have functions which return promises to feed into Promise.all() and that’s part of the painful part.
> 2. Promise.all is not designed for chained promises.
> 
> As far as async/await goes: I’m not a big fan of syntactical sugar to hide cumbersome patterns. That’s not a solution. It’s a band-aid IMO.
> 
> My $0.02,
> Harbs
> 
>> On Jan 21, 2019, at 6:13 PM, szogun1987 <sz...@gmail.com> wrote:
>> 
>> I can't see how your manager better than regular promise API.
>> AsyncManager.push/done can be replaced by Promise.all method, that accepts
>> array of promises and combines theirs result into array.
>> AFAIK in Javascript you can handle error in final "then" method call,
>> intermediate promises passes error further if you don't handle it. If you
>> are using async/await syntax you can surround larger piece of code with
>> try/catch.
>> 
>> 
>> 
>> --
>> Sent from: http://apache-royale-development.20373.n8.nabble.com/
> 


Re: Can we do better than Promises?

Posted by Harbs <ha...@gmail.com>.
Thanks for your thoughts.

You’re right that it’s similar to Promise.all, but there’s a couple of differences:

1. With Promise.all(), you still need to have functions which return promises to feed into Promise.all() and that’s part of the painful part.
2. Promise.all is not designed for chained promises.

As far as async/await goes: I’m not a big fan of syntactical sugar to hide cumbersome patterns. That’s not a solution. It’s a band-aid IMO.

My $0.02,
Harbs

> On Jan 21, 2019, at 6:13 PM, szogun1987 <sz...@gmail.com> wrote:
> 
> I can't see how your manager better than regular promise API.
> AsyncManager.push/done can be replaced by Promise.all method, that accepts
> array of promises and combines theirs result into array.
> AFAIK in Javascript you can handle error in final "then" method call,
> intermediate promises passes error further if you don't handle it. If you
> are using async/await syntax you can surround larger piece of code with
> try/catch.
> 
> 
> 
> --
> Sent from: http://apache-royale-development.20373.n8.nabble.com/


Re: Can we do better than Promises?

Posted by szogun1987 <sz...@gmail.com>.
I can't see how your manager better than regular promise API.
AsyncManager.push/done can be replaced by Promise.all method, that accepts
array of promises and combines theirs result into array.
AFAIK in Javascript you can handle error in final "then" method call,
intermediate promises passes error further if you don't handle it. If you
are using async/await syntax you can surround larger piece of code with
try/catch.



--
Sent from: http://apache-royale-development.20373.n8.nabble.com/

Re: Can we do better than Promises?

Posted by Alex Harui <ah...@adobe.com.INVALID>.
I just use callLater.

Royale should support all kinds of patterns.  I don't think there is any one winner.

HTH,
-Alex

On 1/21/19, 7:25 AM, "Harbs" <ha...@gmail.com> wrote:

    Every time I have to use Promises I want to pull out my hair. It must the the most unintuitive software pattern known to man.
    
    Async/await is somewhat better, but it’s just syntactic sugar and you end up dealing with Promises very often anyway. I also hate “magic”.
    
    The only other pattern I am familiar with when it comes to async execution (besides nested callbacks) is Monads and FP. Functional programming is another one of my aversions. I think it goes along with my aversion to math… ;-)
    
    I was wondering if there might be a “better” pattern for strongly typed languages such as AS3:
    1. A “manager" class which has methods which accepts “Execution” instances which implement an interface.
    2. The manager class would have methods for “addExection”, “addOrderedExecution” etc. The ordered execution could have the previous results automatically inserted into the subsequent execution.
    3. The manager class would have a done method which takes a callback with a “Result” object.
    4. It could also have an error method (or dispatch error events) in the case of errors.
    5. Each “Exectution” would have an “execute” method. It could let the manager know when it was done either by a callback or by dispatching an event.
    
    That would allow code like this:
    
    var manager:AsyncManager = new AsyncManager();
    manager.push(new ExecutionType1("foo"));
    manager.push(new ExecutionType2("foo"));
    manager.push(new ExecutionType3("foo"));
    manager.push(new ExecutionType4("foo"));
    manager.done(function(results:Array):void{
    	for each(var res in results){
    	trace(res);
    	}
    });
    Manager.error(function(err:AsyncError):void{
    	trace(err);
    }
    
    For ordered execution with results, it would be as simple as:
    var manager:AsyncManager = new AsyncManager();
    manager.pushOrdered(new ExecutionType1("foo"));
    manager.pushOrdered(new ExecutionType2("foo"));
    manager.pushOrdered(new ExecutionType3("foo"));
    manager.pushOrdered(new ExecutionType4("foo"));
    
    This feels much easier than promise.then, forced resolving and rejection of promises, etc.
    
    Thoughts?
    Harbs