O'Reilly logo

JavaScript Unlocked by Dmitry Sheiko

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Continuation-passing style

We often need a chain of asynchronous calls, that is, a sequence of tasks where one task is started after another is completed. We are interested in an eventual result of asynchronous calls chain. In this case, we can benefit from Continuation-passing style (CPS). JavaScript has already a built-in Promise object. We use it to create a new Promise object. We put our asynchronous task in the Promise callback and invoke the resolve function of the argument list to notify the Promise callback that the task is resolved:

"use strict"; /** * Increment a given value * @param {Number} val * @returns {Promise} */ var foo = function( val ) { /** * Return a promise. * @param {Function} resolve */ return new Promise(function( resolve ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required