gpt4 book ai didi

javascript - 在所有 promise 都 resolve 之后执行 promise

转载 作者:搜寻专家 更新时间:2023-10-31 23:32:48 24 4
gpt4 key购买 nike

我编写了一些代码来遍历目录,选择所有 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(已在代码的上半部分定义,此处未包含)返回未定义,因为它在所有先前的 promise 都已解决之前运行。

我读过 Promise.all 方法,我应该传递一个 promises 数组,问题是我不知道会返回多少个 promises,在我的代码中“renameTask”是作用的变量作为 promise 接收者。

因此,我需要一些帮助 - 我提前非常感谢 - 了解如何将 forEach 本身作为一个 promise ,并且在所有 forEach promise 得到解决之后,然后执行最终的 promise 。

提前致谢。

更新:

Felix,非常感谢,我现在更近了

// 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 并将该数组传递给 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

关于javascript - 在所有 promise 都 resolve 之后执行 promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com