为承诺写循环的正确方法。

为承诺写循环的正确方法。

如何正确构造循环以确保以下内容承诺电话和锁链logger.log(RES)在迭代过程中同步运行?(蓝鸟)

db.getUser(email).then(function(res) { logger.log(res); }); // this is a promise


我尝试了以下方法http:/blog.victorQun.com/javascript-诺言-时间-循环 )

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');});


虽然它看起来很管用,但我不认为它能保证呼叫的顺序logger.log(RES);


有什么建议吗?


扬帆大鱼
浏览 503回答 3
3回答

哆啦的时光机

我认为它不能保证调用logger.log(Res)的顺序;事实上确实如此。语句在resolve打电话。有什么建议吗?很多。最重要的是使用创建-承诺-手动反模式-只做promiseWhile(…,&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;db.getUser(email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then(function(res)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.log(res);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});})…第二,while功能可以简化很多:var&nbsp;promiseWhile&nbsp;=&nbsp;Promise.method(function(condition,&nbsp;action)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!condition())&nbsp;return; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;action().then(promiseWhile.bind(null,&nbsp;condition,&nbsp;action));});第三,我不会用while循环(带有闭包变量),但是for循环:var&nbsp;promiseFor&nbsp;=&nbsp;Promise.method(function(condition,&nbsp;action,&nbsp;value)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!condition(value))&nbsp;return&nbsp;value; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;action(value).then(promiseFor.bind(null,&nbsp;condition,&nbsp;action));});promiseFor(function(count)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;count&nbsp;<&nbsp;10;},&nbsp;function(count)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;db.getUser(email) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then(function(res)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.log(res);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;++count; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});},&nbsp;0).then(console.log.bind(console,&nbsp;'all&nbsp;done'));

子衿沉夜

下面是我对标准承诺对象的处理方法。//&nbsp;Given&nbsp;async&nbsp;function&nbsp;sayHifunction&nbsp;sayHi()&nbsp;{ &nbsp;&nbsp;return&nbsp;new&nbsp;Promise((resolve)&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;setTimeout(()&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log('Hi'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resolve(); &nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;3000); &nbsp;&nbsp;});}//&nbsp;And&nbsp;an&nbsp;array&nbsp;of&nbsp;async&nbsp;functions&nbsp;to&nbsp;loop&nbsp;throughconst&nbsp;asyncArray&nbsp;=&nbsp;[sayHi,&nbsp;sayHi,&nbsp;sayHi]; &nbsp;&nbsp;//&nbsp;We&nbsp;create&nbsp;the&nbsp;start&nbsp;of&nbsp;a&nbsp;promise&nbsp;chainlet&nbsp;chain&nbsp;=&nbsp;Promise.resolve(); &nbsp;&nbsp;//&nbsp;And&nbsp;append&nbsp;each&nbsp;function&nbsp;in&nbsp;the&nbsp;array&nbsp;to&nbsp;the&nbsp;promise&nbsp;chainfor&nbsp;(const&nbsp;func&nbsp;of&nbsp;asyncArray)&nbsp;{ &nbsp;&nbsp;chain&nbsp;=&nbsp;chain.then(func);}//&nbsp;Output://&nbsp;Hi//&nbsp;Hi&nbsp;(After&nbsp;3&nbsp;seconds)//&nbsp;Hi&nbsp;(After&nbsp;3&nbsp;more&nbsp;seconds)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android
Node.js