在所有承诺得到解决后执行承诺

Gui*_*rmo 3 javascript node.js promise ecmascript-6 es6-promise

我编写了一些迭代目录的代码,选择所有的jpg文件,重命名它们,创建thumb文件夹并将它们放在该文件夹中.我还编写了一个函数,每个图像生成一个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);
Run Code Online (Sandbox Code Playgroud)

});

我的问题是,听起来像逻辑一样,galleryHtml(已在代码的上半部分定义,此处未包含)返回undefined,因为它在所有先前的promise都被解析之前运行.

我已经阅读了Promise.all方法,并且我应该传递一个promises数组,问题是我不知道会返回多少个promise,而在我的代码中"renameTask"是作为promise的变量接收器.

所以,我需要一些帮助 - 我真的很感激 - 要了解如何将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 >>>>");
    });

});
Run Code Online (Sandbox Code Playgroud)

问题是,我永远不会收到消息"<<<<<所有承诺都已解决>>>>",我知道这应该是我失踪的一件小事.再次感谢您的帮助!

Fel*_*ing 7

我已经阅读了Promise.all方法,并且我应该传递一个promises数组,问题是我不知道会返回多少个promise,而在我的代码中"renameTask"是作为promise的变量接收器.

它并不像你想象的那么复杂.创建一个数组,将promises添加到它并将数组传递给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
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案,尽管您可能想添加`.then(results)`以promise数组的顺序返回已解析结果的数组。所以`results [0]`对应于`promises [0]`,依此类推。 (2认同)