在下面的例子中從http://bluebirdjs.com/docs/api/promise.all.htmlBluebird中的Promise.all()是否等待迭代器?
var files = [];
for (var i = 0; i < 100; ++i) {
files.push(fs.writeFileAsync("file-" + i + ".txt", "", "utf-8"));
}
Promise.all(files).then(function() {
console.log("all the files were created");
});
它是確保通過(藍鳥)承諾,我們開始Promise.all()
線或者是用於循環如此之快,我們可以假設他們以前的for循環會完成會在Promise.all()
之前完成嗎?
我想了解我可以期望,以完成,我需要環繞承諾,所以我不寫這樣的事情是什麼時,沒有必要:
some_promise_that_makes_files_array_with_for_loop().then(function(files){
Promise.all(files).then(function() {
console.log("all the files were created");
});
});
'for'循環是同步的,所以它是由單線程保證的,它絕對會在下一行代碼之前完成,而不管它是什麼。 – adeneo
如果沒有藍鳥,那麼這將是微不足道的 - > https://jsfiddle.net/dy3cmart/ – adeneo