如何连续执行承诺? [英] How to execute promises in series?

查看:41
本文介绍了如何连续执行承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var promiseReturningFuncs = [];
for(var i = 0; i < 5; i++){
  promiseReturningFuncs.push(askQuestion);
}

var programmers = [];
Promise.reduce(promiseReturningFuncs, function(resp, x) {
  console.log(typeof resp);
  if(typeof resp != "function") {
    programmers.push(resp);
  }
  return x();
})
.then(function(resp) {
  programmers.push(resp);
  console.log(programmers);
});

我的目标:依次执行askQuestion函数并解析该函数​​创建的对象数组.(此功能必须按顺序执行,以便可以响应用户输入)

My goal: execute the askQuestion function in series and resolve an array of objects created by that function. (this function must execute in series so that it can respond to user input)

因此,想象一下askQuestion函数返回一个可以解析要添加到数组中的对象的Promise.

So imagine that the askQuestion function returns a promise that resolves a object I want to add to an array.

这是我做事的凌乱方式.我正在寻找找到一种更清洁的方法,理想情况下,我什至不需要推送到数组,而只有一个最终的.then,其中响应是一个数组.

This is my messy way of doing it. I am looking to find a cleaner way of doing it, ideally, i wouldn't even need to push to an array, I would just have a final .then, where the response is an array.

推荐答案

由于您似乎正在使用Bluebird Promise库,因此有许多内置选项可用于对Promise返回函数进行排序.您可以使用并发值为1的 Promise.reduce() Promise.map() Promise.mapSeries Promise.each().如果迭代器函数返回了一个Promise,则所有这些都将等待下一次迭代,直到该Promise得以解决.使用哪种方法更多地取决于数据的结构方式和所需的结果(实际上都不会显示或描述).

Since you appear to be using the Bluebird promise library, you have a number of built-in options for sequencing your promise returning functions. You can use Promise.reduce(), Promise.map() with a concurrency value of 1, Promise.mapSeries or Promise.each(). If the iterator function returns a promise, all of these will wait for the next iteration until that promise resolves. Which to use depends more upon the mechanics of how your data is structured and what result you want (neither of which you actually show or describe).

让我们假设您有一系列的promise返回函数,并且您想一次调用它们,等待一个函数解析,然后再调用下一个.如果您想要所有结果,那么我建议 Promise.mapSeries():

Let's suppose you have an array of promise returning functions and you want to call them one at a time, waiting for the one to resolve before calling the next one. If you want all the results, then I'd suggest Promise.mapSeries():

let arrayOfPromiseReturningFunctions = [...];

// call all the promise returning functions in the array, one at a time
// wait for one to resolve before calling the next
Promise.mapSeries(arrayOfPromiseReturningFunctions, function(fn) {
    return fn();
}).then(function(results) {
     // results is an array of resolved results from all the promises
}).catch(function(err) {
     // process error here
});

也可以使用

Promise.reduce(),但它会累加一个结果,将结果从一个传递到下一个,并以一个最终结果结束(如 Array.prototype.reduce()确实).

Promise.reduce() could also be used, but it would accumulate a single result, passing it from one to the next and end with one final result (like Array.prototype.reduce() does).

Promise.map() Promise.mapSeries()的更通用版本,可让您控制并发数(在同时).

Promise.map() is a more general version of Promise.mapSeries() that lets you control the concurrency number (the number of async operations in flight at the same time).

Promise.each()也会对您的函数进行排序,但不会累加结果.它假定您没有结果,或者您正在带外或通过副作用累积结果.我倾向于不喜欢使用 Promise.each(),因为我不喜欢副作用编程.

Promise.each() will also sequence your functions, but does not accumulate a result. It assumes you either don't have a result or you are accumulating the result out-of-band or via side effects. I tend to not like to use Promise.each() because I don't like side effect programming.

这篇关于如何连续执行承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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