程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Node JS Promise.all和forEach大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Node JS Promise.all和forEach?

开发过程中遇到Node JS Promise.all和forEach的问题如何解决?下面主要结合日常开发的经验,给出你关于Node JS Promise.all和forEach的解决方法建议,希望对你解决Node JS Promise.all和forEach有所启发或帮助; @H_502_0@使用一些简单的规则非常简单:

  • -您不返回的任何承诺都不会在外面等待。
  • 这种方式等待所有promise,并且其中任何一个的错误都不会消失。
  • - then链通常最多为1层深。
  • -应该在承诺中,或者应该使用承诺来表明其完成。
@H_502_0@还有一些提示:

  • - -如果您要通过函数映射值,则@H_784_9@map可以简洁地表达一个动作一一应用并汇总结果的概念。
  • -并发执行并等待它们Promise.all比一个接一个地执行要好-每个等待都在下一个执行之前要好。
@H_502_0@好的,让我们开始吧:

var items = [1, 2, 3, 4, 5];
var fn = function asyncMultiplyBy2(v){ // sample async action
    return new Promise(resolve => setTimeout(() => resolve(v * 2), 100));
};
// map over forEach since it returns

var actions = items.map(fn); // run the function over all items

// we Now have a promises array and we want to wait for it

var results = Promise.all(actions); // pass array of promises

results.then(data => // or just .then(console.log)
    console.log(data) // [2, 4, 6, 8, 10]
);

// we can nest this of course, as I saID, `then` chains:

var res2 = Promise.all([1, 2, 3, 4, 5].map(fn)).then(
    data => Promise.all(data.map(fn))
).then(function(data){
    // the next `then` is executed after the promise has returned from the prevIoUs
    // `then` fulfilled, in this case it's an aggregate promise because of 
    // the `.all` 
    return Promise.all(data.map(fn));
}).then(function(data){
    // just for good measure
    return Promise.all(data.map(fn));
});

// Now to get the results:

res2.then(function(data){
    console.log(data); // [16, 32, 48, 64, 80]
});

解决方法

我有一个类似结构的数组,它公开了异步方法。异步方法调用返回数组结构,从而返回更多异步方法。我正在创建另一个JSON对象来存储从该结构获得的值,因此我需要注意跟踪回调中的引用。

我已经编写了一个蛮力解决方案,但是我想学习一个更惯用或干净的解决方案。

  1. 对于n级嵌套,该模式应该是可重复的。
  2. 我需要使用promise.all或一些类似的技术来确定何时解析封闭例程。
  3. 并非每个元素都必然涉及进行异步调用。因此,在嵌套promise.all中,我不能仅仅基于索引对我的JSON数组元素进行分配。不过,我确实需要在嵌套的forEach中使用诸如promise.all之类的内容,以确保在解析封闭例程之前已进行了所有属性分配。
  4. 我正在使用bluebird promise lib,但这不是必需的

这是一些部分代码-

var jsonItems = [];

items.forEach(function(item){

  var jsonItem = {};
  jsonItem.name = item.name;
  item.getThings().then(function(things){
  // or Promise.all(allItemGetThingCalls,function(things){

    things.forEach(function(thing,indeX){

      jsonItems[index].thingName = thing.name;
      if(thing.type === 'file'){

        thing.getFile().then(function(filE){ //or promise.all?

          jsonItems[index].filesize = file.getSize();

大佬总结

以上是大佬教程为你收集整理的Node JS Promise.all和forEach全部内容,希望文章能够帮你解决Node JS Promise.all和forEach所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。