javascript - Converting node async code to promises -


i'm experimenting promises - namely when.js - , want convert test code - little unclear how after reading docs. experiments far have been far messier standard callback pyramids think missing shortcut.

here's sample code replicate:

async1(function(err, res) {   res++;   async2(res, function(error, result) {     done();   }) }) 

nodefn.call(async2, nodefn.call(async1)).ensure(done); 

here, async2 gets called synchronously , promise async1() argument - doesn't wait async1 resolve. chain them, need use

nodefn.call(async1).then(nodefn.lift(async2)).ensure(done); // equivalent to: nodefn.call(async1).then(function(result) {     return nodefn.call(async2, result); }).ensure(done); 

i want perform logic between 2 calls

then need put function in chain, or modify 1 of functions in chain:

nodefn.call(async1)   .then(function(res){return res+1;}) // return modified result   .then(nodefn.lift(async2))   .ensure(done); // or nodefn.call(async1).then(function(res) {     res++; // whatever want     return nodefn.call(async2, res); }).ensure(done); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -