Vadim Dalecky1 year agoCallbacks are significantly faster than Promises in Node.js. How can we get the performance of callbacks while benefiting from the Promise and async/await syntax?
JavaScript could introduce CallbackAwaitExpression, which syntactically would look very much like the existing AwaitExpression but it would operate on top of callbacks instead of promises.
The callbacks would have the following form:
The new CallbackAwaitExpression would have an extra identifier parameter of type Callback, syntactically in between the await keyword and the expression being awaited, for example, note the cb identifier:
Likewise, the async function syntax would also be extended allow AsyncCallbackFunction type. There as well, the syntax would allow a single callback identifier:
Putting this all together, this would allow to write async/await syntax-powered code, while benefiting from the performance of callbacks. It would allow to write functions like:
The code would be equivalent to the existing JavaScript:
Here is how the above code can look like using the existing async/await syntax. The code is almost equivalent to the async/await callback proposal, but less performant due to Promise usage.