javascript - resolve values in a Promise While Loop -


i found: correct way write loops promise. , while loop using bluebird promises

however when try use these loop promise, resolve value of promise not passed down chain.

for example, following code prints:

did 1 times
did 2 times
undefined <-- should hello world

var promise = require('bluebird');  var times = 0;  var do_it = function(o) {     return new promise(function(resolve, reject) {         settimeout(function () {             console.log("did %d times", ++times);             resolve(o);         }, 1000);     }); }  var promisewhile = promise.method(function(condition, action) {     if (!condition()) return;     return action().then(promisewhile.bind(null, condition, action)); });  var do_it_twice = function(o) {     return promisewhile(         function() { return times < 2; },         function() { return do_it(o);  }     ); }  do_it_twice("hello world").then(function(o) {console.log(o)}); 

you need specify returned value

var promisewhile = promise.method(function(condition, action, result) {     if (!condition()) return result;     return action().then(promisewhile.bind(null, condition, action, result)); });  var do_it_twice = function(o) {     return promisewhile(         function() { return times < 2; },         function() { return do_it(o);  },         o     ); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -