简体   繁体   English

快速构建Promise.all()的承诺数组

[英]Building an array of promises for Promise.all() on the fly

so I've been trying to use the Promise.all() method and I'm running into an issue where I need to pass the method an array of promises, but I'm not sure how many promises are going to be called for/needed by the application. 所以我一直在尝试使用Promise.all()方法,但是我遇到了一个问题,我需要将该方法传递给一组Promise.all() ,但是我不确定要调用多少个Promise /应用程序所需。

Here's a method that I've gotten working by specifically building the array 这是我通过专门构建数组来工作的一种方法

router.post('/test1', (req, res, next) => {
    console.log('Hit on POST /test1');
    let arr = ['email1@email.com','email2@email.com'];
    Promise.all([
      lookUpUserId(arr[0]),
      lookUpUserId(arr[1])
    ]).then((x)=>{res.send(x)});
});

My lookUpUserId() function is returning a promise and I'm able to use it if I explicitly list them in the array. 我的lookUpUserId()函数返回一个promise,如果我在数组中明确列出它们,我就可以使用它。 However I've tried a few methods of creating this array on the fly and I can't seem to get it working. 但是,我尝试了几种动态创建此数组的方法,但似乎无法正常工作。
Example #1 例子1

router.post('/test1', (req,res,next) => {
    console.log('Hit on POST /test1');
    let arr = ['email1@email.com','email2@email.com'];
    Promise.all((arr)=>{
      let out = [];
      for(let i=0; i<arr.length; i++){
        out.push(lookUpUserId(arr[i]));
      }
      return out;
    }).then((x)=>{res.send(x)});
});

Example 2 例子2

outer.post('/test1', (req,res,next) => {
    console.log('Hit on POST /test1');
    let arr = ['email1@email.com','email2@email.com'];
    let promArr = (arr)=>{
      let out = [];
      for(let i=0; i<arr.length; i++){
        out.push(lookUpUserId(arr[i]));
      }
    }
    Promise.all(promArr).then((x)=>{res.send(x)});
});

You can use Array.prototype.map() where the first parameter to lookUpUserId function is expected to be an element of arr 您可以使用Array.prototype.map() ,其中lookUpUserId函数的第一个参数应该是arr的元素

router.post('/test1', (req, res, next) => {
    console.log('Hit on POST /test1');
    let arr = ['email1@email.com','email2@email.com'];
    Promise.all(arr.map(lookUpUserId))
    .then(x => res.send(x));
});

The issue with second approach is that you do not call the function, pass the array as parameter to the function, or return the out array from the function 第二种方法的问题是您不调用函数,不将数组作为参数传递给函数,也不从函数返回out数组。

outer.post('/test1', (req,res,next) => {
    console.log('Hit on POST /test1');
    let arr = ['email1@email.com','email2@email.com'];
    let promArr = arr => {
      let out = [];
      for(let i=0; i<arr.length; i++) {
        out.push(lookUpUserId(arr[i]));
      }
      return out // `return` the array of `Promise` objects
    }
    Promise.all(promArr(arr)).then(x => res.send(x));
});

It's the same problem in both scenarios....your not actually invoking the function call therefore you never pass the array to Promise.all. 在这两种情况下都是相同的问题。...您实际上没有调用函数调用,因此您永远不会将数组传递给Promise.all。

Using the second example, purely because it's easier to demonstrate the fix, try: 使用第二个示例,纯粹是因为它更容易演示此修复程序,请尝试:

Promise.all(promAll(arr))
    .then(...)
    .catch(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM