nodejs async.whilst只会调用一次

时间:2017-01-03 01:50:27

标签: javascript node.js async.js

正如问题所述,async.whilst只会调用一次。但是,如果我传递一个包含2个对象(长度为2)的数组,它只会调用一次 - 而不是为数组的每个索引调用一次。

//if previous awsKeys detected in req.body / images are detected : delete them.
    exports.multipleDelete = function (req, res, next) {
        var body = req.body;
      //if object already has existing keys, they will be in the body.existingKeys array.
        var awsKeyTrash = body.existingKeys;
        if (awsKeyTrash !== undefined) {
            var j = 0;
            async.whilst(
                function () {
                    return j < awsKeyTrash.length;
                },
                function () {
                    var picInfo = awsKeyTrash[j];
                    s3.deleteObject({
                        Bucket: aws.bucket,
                        Key: picInfo.key
                    }, function (err, data) {
                        if (err) {
                            console.log('delete err', err);
                    //if there is an error, set pic information to original
                            req.body[picInfo.name] = picInfo.picUrl;
                            req.body[picInfo.key] = picInfo.awsKeyVal;
                            j++;
                        };
                        console.log('deleted')
                        console.log('j ', j)
                        j++;
                        res.send('deleted');
                    });
                },
                function (err) {
            console.log('profile edit , pic delteion err : ', err);
            return res.status(409).send({
              message: ['Unable to edit profile at this time. Try again']
            });
                })
            next();
        }
        else {
            next();
        }
    }

这是body.existingKeys数组的一个例子:

     Array[
        {
        awsKeyVal: "users/66085aa8-6501-4f90-973c-1b18edf4087eScreenShot2016-12-05at10.03.07PM.png",
        key: "awsPictureKey",
        name: "picture",
        picUrl: "https://example-bucket.s3.amazonaws.com/users/66085aa8-6501-4f90-973c-1b18edf4087eScreenShot2016-12-05at10.03.07PM.png"
    }, 
        {
        awsKeyVal: "coverphoto/7180d1ae-748c-4b96-86cb-5cb29cebdc9bScreenShot2016-12-10at3.13.18PM.png",
        key: "awsCoverKey",
        name: "cover",
        picUrl: "https://example-bucket.s3.amazonaws.com/coverphoto/7180d1ae-748c-4b96-86cb-5cb29cebdc9bScreenShot2016-12-10at3.13.18PM.png"
    }]

1 个答案:

答案 0 :(得分:1)

async.whylist接听你不打电话的回电。

您的next()看起来也是错位的。

此外,最后一个函数不是错误处理程序。它只是将错误作为第一个参数,如果没有错误,则可以为null。

async.whilst(
    function () {
        return j < awsKeyTrash.length;
    },
    function ( callback ) {
        var picInfo = awsKeyTrash[j];
        s3.deleteObject({
            Bucket: aws.bucket,
            Key: picInfo.key
        }, function (err, data) {
            if (err) {
                console.log('delete err', err);
                //if there is an error, set pic information to original
                req.body[picInfo.name] = picInfo.picUrl;
                req.body[picInfo.key] = picInfo.awsKeyVal;
                j++;
            };
            console.log('deleted')
            console.log('j ', j)
            j++;
            res.send('deleted');

            callback();
        });
    },
    function (err) {

        if( err ) {
            console.log('profile edit , pic delteion err : ', err);
            return res.status(409).send({
                message: ['Unable to edit profile at this time. Try again']
            });
        }

        // If someone else handles errors, pass it on here. Otherwise,
        // throw or something else to block program flow
        next(err);
    }
);