为承诺写循环的正确方法。
db.getUser(email).then(function(res) { logger.log(res); }); // this is a promise
var Promise = require('bluebird');var promiseWhile = function(condition, action) { var resolver = Promise.defer(); var loop = function() { if (!condition()) return resolver.resolve(); return Promise.cast(action()) .then(loop) .catch(resolver.reject); }; process.nextTick(loop); return resolver.promise;});var count = 0;promiseWhile(function() { return count < 10;}, function() { return new Promise(function(resolve, reject) { db.getUser(email) .then(function(res) { logger.log(res); count++; resolve(); }); }); }).then(function() { console.log('all done');});
哆啦的时光机
子衿沉夜