顺序运行Bluebird Promises,没有返回值? [英] Run Bluebird Promises Sequentially, without return values?

查看:38
本文介绍了顺序运行Bluebird Promises,没有返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已通过多种方式(不是很简单)提出了这个问题.

如何重写此Promise.all,使promise1完全在promise2之前运行?

How would this Promise.all be rewritten so that promise1 runs completely before promise2?

var promise1 = function() { .. lots of promise stuff };
var promise2 = function() { .. lots more promise stuff };

Promise.all([promise1, promise2]).then(function() {
  log.info("ran promise1 & promise2");
});

Promise.all运行promise1& Promise2并行.

推荐答案

您可以将Promise.map的并发选项设置为1.

You can use Promise.map with concurrency option set to 1.

var promise1 = function () {
    return new Promise(function (resolve, reject) {
        console.log("promise1 pending");
        setTimeout(function () {
            console.log("promise1 fulfilled");
            resolve();
        }, 1000)
    })
};

var promise2 = function () {
    return new Promise(function (resolve, reject) {
        console.log("promise2 pending");
        setTimeout(function () {
            console.log("promise2 fulfilled");
            resolve()
        }, 50)
    })
};

Promise.map([promise1, promise2], function (promiseFn) {
    return promiseFn(); //make sure that here You return Promise
}, {concurrency: 1}); //it will run promises sequentially 

//It logs
//promise1 pending
//promise 1 fulfilled
//promise2 pending
//promise 2 fulfilled

这篇关于顺序运行Bluebird Promises,没有返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆