Proposal: use "One More Point" to handle async functions

Li Xiaolong 898310778 at outlook.com
Tue Sep 27 14:09:31 UTC 2016


The traditional callback method to handle async is not elegant and clear.
Even the promised style is kind of odd. So I got an idea that we use "One
More Point"(OMP) to make async coding easier to use. For example: 

```js

//Without One More Point:

function async(args, callback) {

         ...

         obj.doSomeThing(function(result) {

                   obj2.doSomeOtherThing(result, function(result2){

                            callback(result2);

})

         });

}

async([1,2,3], function(result) {

         alert(result);

.

});

 

 

//With the help of One More Point:

function async(args) {

         ...

         .return obj2..doSomeOtherThing(obj..doSomeThing());

}

alert(.async([1,2,3]));

.

```

So, in general, the OMP makes async coding looks like sync coding. The
return value of a function called with OMP will return the value after the
".return" keyword. The code after the OMP call will all be delayed until the
async ends. This sounds like the code is blocked, but actually it's
non-blocking, as the user can still interact with the interface and the cpu
is sleeping. There is one limitation that OMP style can only replace
callback style async calls that are at the end of a function, since there is
no more code after the async function is called. However, as far as I can
see, about half or more async functions are at the end of a function. So,
the OMP is significant.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160927/4c6e3ef6/attachment.html>


More information about the es-discuss mailing list