Promise.all与嵌套的Promise.all

时间:2021-10-03 20:09:41

I've nested arrays, I'm able to retrieve promises for the 2nd level array but not sure how to implement a then once top level finishes as well.

我已经嵌套了数组,我能够检索第二级数组的promises,但不知道如何实现一次*数组的完成。

result.forEach(function(entity){ // outer list ???
    return Promise.all(entity.urls.map(function(item){
        return requestURL(item.href);
    }));
});

for instance if results has two or more items and each item has 10 or more urls to fetch, how would we implement then of [Promise.all][1] for all the promises. Native solution please.

例如,如果结果有两个或多个项目,并且每个项目有10个或更多要获取的网址,那么我们如何为所有承诺实现[Promise.all] [1]。原生方案请。

Basically to handle nested arrays of promises in a right way.

基本上以正确的方式处理嵌套的promises数组。

Data Structure:

var result = [
    {
        urls: [
            {href: "link1"},
            {href: "link2"},
            {href: "link3"}
        ]
    },
    {
        urls: [
            {href: "link4"},
            {href: "link5"},
            {href: "link6"}
        ]
    }
];

1 个解决方案

#1


14  

Use map instead of forEach, and wrap it inside another Promise.all call.

使用map而不是forEach,并将其包装在另一个Promise.all调用中。

var arr = [
  {subarr: [1,2,3]},
  {subarr: [4,5,6]},
  {subarr: [7,8,9]}
];
function processAsync(n) {
  return new Promise(function(resolve) {
    setTimeout(
      function() { resolve(n * n); },
      Math.random() * 1e3
    );
  });
}
Promise.all(arr.map(function(entity){
  return Promise.all(entity.subarr.map(function(item){
    return processAsync(item);
  }));
})).then(function(data) {
  console.log(data);
});

You can also use an immediately invoked generator. For example, to get flattened results,

您还可以使用立即调用的生成器。例如,要获得扁平的结果,

var arr = [
  {subarr: [1,2,3]},
  {subarr: [4,5,6]},
  {subarr: [7,8,9]}
];
function processAsync(n) {
  return new Promise(function(resolve) {
    setTimeout(
      function() { resolve(n * n); },
      Math.random() * 1e3
    );
  });
}
Promise.all(function*() {
  for(var entity of arr)
    for(var item of entity.subarr)
      yield processAsync(item);
}()).then(function(data) {
  console.log(data);
});

#1


14  

Use map instead of forEach, and wrap it inside another Promise.all call.

使用map而不是forEach,并将其包装在另一个Promise.all调用中。

var arr = [
  {subarr: [1,2,3]},
  {subarr: [4,5,6]},
  {subarr: [7,8,9]}
];
function processAsync(n) {
  return new Promise(function(resolve) {
    setTimeout(
      function() { resolve(n * n); },
      Math.random() * 1e3
    );
  });
}
Promise.all(arr.map(function(entity){
  return Promise.all(entity.subarr.map(function(item){
    return processAsync(item);
  }));
})).then(function(data) {
  console.log(data);
});

You can also use an immediately invoked generator. For example, to get flattened results,

您还可以使用立即调用的生成器。例如,要获得扁平的结果,

var arr = [
  {subarr: [1,2,3]},
  {subarr: [4,5,6]},
  {subarr: [7,8,9]}
];
function processAsync(n) {
  return new Promise(function(resolve) {
    setTimeout(
      function() { resolve(n * n); },
      Math.random() * 1e3
    );
  });
}
Promise.all(function*() {
  for(var entity of arr)
    for(var item of entity.subarr)
      yield processAsync(item);
}()).then(function(data) {
  console.log(data);
});