在所有 promise 都 resolve 之后执行 promise
Execute promise after all promises were resolved
我编写了一些代码来遍历目录,选择所有 jpg 文件,重命名它们,创建 thumbs 文件夹并将它们放入该文件夹中。
我还编写了一个函数,该函数为每个图像生成一块 html,并且在 forEach 循环结束时应该将所有输出写入一个文件。
// Read the directory
fs.readdir(dir, function (err, files) {
// Return the error if something went wrong
if (err) {
console.log("Something went wrong");
}
//Make thumbs dir
fs.mkdirSync(thumbsFolder);
// For every file in the list
files.forEach(function (file) {
var extension = path.extname(file);
if (extension === ".jpg") {
//Rename images
var renameTask = renameImages(file, newFileName, extension);
//Generating thumbs
renameTask.then(function (newFileName) {
generateThumbs(dir, newFileName, thumbsFolder)
console.log("Promise resolved! Generating thumb for: " + newFileName);
galleryHtml += generateThumbsGallery(newFileName, articleTitle);
}, function (err) {
console.log(err);
});
}
});
//Gallery output
console.log("Gallery Output: ");
console.log(galleryHtml);
});
我的问题是,听起来合乎逻辑,galleryHtml(已在代码的上半部分定义,此处未包含),returns未定义,因为它在所有先前的承诺之前运行已解决。
我读过 Promise.all 方法,我应该传递一个承诺数组,问题是我不知道会返回多少承诺,在我的代码中 "renameTask" 是充当承诺接收者的变量。
所以,我需要一些帮助 - 我提前非常感谢 - 了解如何将 forEach 本身作为一个承诺,并且在所有 forEach 承诺得到解决之后,然后执行最终的承诺。
提前致谢。
更新:
菲利克斯,非常感谢,我现在更近了
// Read the directory
fs.readdir(dir, function (err, files) {
// Return the error if something went wrong
if (err) {
console.log("Something went wrong");
}
var promises = [];
//Make thumbs dir
fs.mkdirSync(thumbsFolder);
// For every file in the list
files.forEach(function (file) {
var extension = path.extname(file);
if (extension === ".jpg") {
//Rename images
var renameTask = renameImages(file, newFileName, extension);
promises.push(renameTask.then(function (newFileName) {
generateThumbs(dir, newFileName, thumbsFolder)
console.log("Promise resolved! Generating thumb for: " + newFileName);
galleryHtml += generateThumbsGallery(newFileName, articleTitle);
}, function (err) {
console.log(err);
})
);
console.log("<<<< Promises length: " + promises.length + " >>>>");
}
});
Promise.all(promises).then(function () {
console.log("<<<<< All promises were resolved >>>>");
});
});
问题是我从来没有收到消息“<<<<< All promises were resolved >>>>”,我知道这应该是我遗漏的一件小事。再次感谢求助!
I've read about Promise.all method, and that I should pass a promises array, the question is that I don´t know how many promises would be returned, and in my code "renameTask" is the variable that acts as the promise receiver.
它并不像您想象的那么复杂。创建一个数组,向其添加承诺并将该数组传递给 Promise.all
:
var promises = []; // array of promises
files.forEach(function (file) {
var extension = path.extname(file);
if (extension === ".jpg") {
var renameTask = renameImages(file, newFileName, extension);
promises.push(renameTask.then(...)); // add promise
}
});
Promise.all(promises).then(...); // use array
我编写了一些代码来遍历目录,选择所有 jpg 文件,重命名它们,创建 thumbs 文件夹并将它们放入该文件夹中。 我还编写了一个函数,该函数为每个图像生成一块 html,并且在 forEach 循环结束时应该将所有输出写入一个文件。
// Read the directory
fs.readdir(dir, function (err, files) {
// Return the error if something went wrong
if (err) {
console.log("Something went wrong");
}
//Make thumbs dir
fs.mkdirSync(thumbsFolder);
// For every file in the list
files.forEach(function (file) {
var extension = path.extname(file);
if (extension === ".jpg") {
//Rename images
var renameTask = renameImages(file, newFileName, extension);
//Generating thumbs
renameTask.then(function (newFileName) {
generateThumbs(dir, newFileName, thumbsFolder)
console.log("Promise resolved! Generating thumb for: " + newFileName);
galleryHtml += generateThumbsGallery(newFileName, articleTitle);
}, function (err) {
console.log(err);
});
}
});
//Gallery output
console.log("Gallery Output: ");
console.log(galleryHtml);
});
我的问题是,听起来合乎逻辑,galleryHtml(已在代码的上半部分定义,此处未包含),returns未定义,因为它在所有先前的承诺之前运行已解决。
我读过 Promise.all 方法,我应该传递一个承诺数组,问题是我不知道会返回多少承诺,在我的代码中 "renameTask" 是充当承诺接收者的变量。
所以,我需要一些帮助 - 我提前非常感谢 - 了解如何将 forEach 本身作为一个承诺,并且在所有 forEach 承诺得到解决之后,然后执行最终的承诺。
提前致谢。
更新:
菲利克斯,非常感谢,我现在更近了
// Read the directory
fs.readdir(dir, function (err, files) {
// Return the error if something went wrong
if (err) {
console.log("Something went wrong");
}
var promises = [];
//Make thumbs dir
fs.mkdirSync(thumbsFolder);
// For every file in the list
files.forEach(function (file) {
var extension = path.extname(file);
if (extension === ".jpg") {
//Rename images
var renameTask = renameImages(file, newFileName, extension);
promises.push(renameTask.then(function (newFileName) {
generateThumbs(dir, newFileName, thumbsFolder)
console.log("Promise resolved! Generating thumb for: " + newFileName);
galleryHtml += generateThumbsGallery(newFileName, articleTitle);
}, function (err) {
console.log(err);
})
);
console.log("<<<< Promises length: " + promises.length + " >>>>");
}
});
Promise.all(promises).then(function () {
console.log("<<<<< All promises were resolved >>>>");
});
});
问题是我从来没有收到消息“<<<<< All promises were resolved >>>>”,我知道这应该是我遗漏的一件小事。再次感谢求助!
I've read about Promise.all method, and that I should pass a promises array, the question is that I don´t know how many promises would be returned, and in my code "renameTask" is the variable that acts as the promise receiver.
它并不像您想象的那么复杂。创建一个数组,向其添加承诺并将该数组传递给 Promise.all
:
var promises = []; // array of promises
files.forEach(function (file) {
var extension = path.extname(file);
if (extension === ".jpg") {
var renameTask = renameImages(file, newFileName, extension);
promises.push(renameTask.then(...)); // add promise
}
});
Promise.all(promises).then(...); // use array