Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 环内承诺链_Javascript_Node.js_Promise_Bluebird - Fatal编程技术网

Javascript 环内承诺链

Javascript 环内承诺链,javascript,node.js,promise,bluebird,Javascript,Node.js,Promise,Bluebird,cleanupInstance也是一个承诺链。然而,目前我的for循环在整个承诺链完成之前进入下一个迭代。有没有一种方法也能保证循环?我正在使用蓝鸟图书馆(nodejs)进行承诺。您可以使用: 为什么不使用Promise.each或Promise.all?这将更加容易理解和灵活 请检查下面的例子 var Promise = require('bluebird'); ... Promise.each(listofInstances, function(instance) { return cl

cleanupInstance
也是一个承诺链。然而,目前我的for循环在整个承诺链完成之前进入下一个迭代。有没有一种方法也能保证循环?我正在使用蓝鸟图书馆(nodejs)进行承诺。

您可以使用:


为什么不使用
Promise.each
Promise.all
?这将更加容易理解和灵活

请检查下面的例子

var Promise = require('bluebird');
...
Promise.each(listofInstances, function(instance) {
  return cleanupInstance(instance).then(function() {
    console.log('Done', instance);
  });
}).then(function() {
  console.log('Done with all instances');
});
或者您可能有循环中的循环。如果你有一个数组的数组,那就举个例子

var Promise = require('bluebird');
var someArray = ['foo', 'bar', 'baz', 'qux'];

Promise.all(someArray.map(function(singleArrayElement){
  //do something and return
  return doSomethingWithElement(singleArrayElement);
})).then(function(results){
  //do something with results
});

Promise.each(someArray, function(singleArrayElement){
  //do something and return
  return doSomethingWithElement(singleArrayElement);
}).then(function(results){
  //do something with results
});

请解释当all循环中有多个承诺链时,代码将是什么。
例如:

var Promise = require('bluebird');
var arrayOfArrays = [['foo', 'bar', 'baz', 'qux'],['foo', 'bar', 'baz', 'qux']];

function firstLoopPromise(singleArray){
  return Promise.all(singleArray.map(function(signleArrayElement){
    //do something and return
    return doSomethingWithElement(signleArrayElement);
  }));
}


Promise.all(arrayOfArrays.map(function(singleArray){
  //do something and return
  return firstLoopPromise(singleArray);
})).then(function(results){
  //do something with results
});
var Promise = require('bluebird');
var arrayOfArrays = [['foo', 'bar', 'baz', 'qux'],['foo', 'bar', 'baz', 'qux']];

function firstLoopPromise(singleArray){
  return Promise.all(singleArray.map(function(signleArrayElement){
    //do something and return
    return doSomethingWithElement(signleArrayElement);
  }));
}


Promise.all(arrayOfArrays.map(function(singleArray){
  //do something and return
  return firstLoopPromise(singleArray);
})).then(function(results){
  //do something with results
});
Promise.all(someArray.map(function(singleArrayElement){
    //do something and return
    return doSomethingWithElement(singleArrayElement)
    .then(function(result){
    return doSomethingWithElement(result)
})
.then(function(result){
    return doSomethingWithElement(result)
})
})).then(function(results){
    //do something with results
});