javascript - Nesting callbacks for more than two tasks -


it possible determine order of two tasks using callbacks, shown below.

a(b);  function a(callback) {    //    callback(); }  function b() {    // next } 

see fiddle

first a(), b().

i concatenate more 2 tasks. i´m dealing quite big functions, i´m looking that:

a(b(c)); 

first a(), b(), c(). i'm not successful this. see fiddle

is there easy way so, maybe without needing promises?

you're calling b immediately, not passing callback a. you'll need use function expression:

a(function(aresult) {     b(c); }); 

of course, can avoid these returning closures functions:

function a(callback) {     return function(args) {         //         if (callback) callback(res);     }; } function b(callback) {     return function(aresult) {         // next         if (callback) callback(res);     }; } function c(callback) {     return function(bresult) {         // next         if (callback) callback(res);     }; } 

which call this:

a(b(c())(); 

(this known pure continuation passing style)


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -