javascript - 如何在 Node.js 中跳过 "async.forEachOf"循环迭代

标签 javascript node.js asynchronous

一个async.waterfall嵌套在 async.forEachOfLimit 内循环如下面的代码所示。

问题:当代码在 async.waterfall 内执行步骤时,如何跳过 async.forEachLimit 的迭代?换句话说,突破 async.waterfall 并返回 async.forEachLimit。我已经在代码中注释了应该进行此检查的位置

当前代码给出错误回调已被调用。

此外,当我想突破 async.waterfall 时,如果我使用 return callback() 代替 cb() ,没有发生错误,但没有跳过。

var async = require('async')
var users = ['a','b','c']

async.forEachOfLimit(users, 1, function(user, index, cb) {

    console.log(index + ': ' + user)

    async.waterfall([
        function(callback) {
            callback(null);
        },
        function(callback) {
            // Skip async.forEAchOfLimit iteration when index == 1
            if(index == 1)
                cb()

            callback(null);
        }
    ], function (err, result) {
        console.log(index + ": done")
        cb()
    });

}, function() {
    console.log('ALL done')
})

错误

0: a
0: done
1: b
2: c
1: done

/Users/x/test/node_modules/async/lib/async.js:43
            if (fn === null) throw new Error("Callback was already called.");
                                   ^
Error: Callback was already called.

所需输出

0: a
0: done
1: b
2: c
2: done
ALL done
<小时/>

使用返回回调()

var async = require('async')
var users = ['a','b','c']

async.forEachOfLimit(users, 1, function(user, index, cb) {

    console.log(index + ': ' + user)

    async.waterfall([
        function(callback) {
            callback(null);
        },
        function(callback) {
            // Skip async.forEAchOfLimit iteration when index == 1
            if(index == 1)
                return callback()

            callback(null);
        }
    ], function (err, result) {
        console.log(index + ": done")
        cb()
    });

}, function() {
    console.log('ALL done')
})

输出

没有爆发...

0: a
0: done
1: b
1: done
2: c
2: done
ALL done

最佳答案

在您的第一个解决方案中,当索引匹配 1 时,cb 被调用两次,这就是为什么您不断收到 Callback was said 错误。尽管您调用了 forEachOfLimit 回调 cb,但您的代码不会停止执行并调用回调。在回调函数中,cb 又被执行一次。

var async = require('async')
var users = ['a','b','c']

async.forEachOfLimit(users, 1, function(user, index, cb) {

    console.log(index + ': ' + user)

    async.waterfall([
        function(callback) {
            callback(null);
        },
        function(callback) {
            // Skip async.forEAchOfLimit iteration when index == 1
            if(index == 1)
                cb() // First callback call

            callback(null);
        }
    ], function (err, result) {
        console.log(index + ": done")
        cb() // Second callback call
    });

}, function() {
    console.log('ALL done')
})

在第二个解决方案中,如果索引匹配 1,它将调用不带参数的回调,并跳过使用空参数调用回调。仍然没有突破 waterfall 。

要使用 waterfall 解决您的问题,您有两种选择。

  1. 使用错误参数调用 waterfall 的方法回调,该方法会脱离 waterfall 并在 waterfall 的回调中处理此错误。

    var async = require('async')
    var users = ['a','b','c']
    
    async.forEachOfLimit(users, 1, function(user, index, cb) {
    
        console.log(index + ': ' + user)
    
        async.waterfall([
            function(callback) {
                callback(null);
            },
            function(callback) {
                // Skip async.forEAchOfLimit iteration when index == 1
                if(index == 1)
                    return callback(new Error('Index equals 1'));
    
                callback(null);
            }
        ], function (err, result) {
            console.log(index + ": done")
    
            if(err.message == 'Index equals 1') {
                err = null; // If you want to continue executing forEachOfLimit no error must be passed to cb
            }
    
            cb(err, result);
        });
    
    }, function() {
        console.log('ALL done')
    });
    
  2. 在每个 waterfall 式方法的开头,跳过其余代码并立即调用回调(这是您在第二次尝试中所做的)

    var async = require('async')
    var users = ['a','b','c']
    
    async.forEachOfLimit(users, 1, function(user, index, cb) {
    
        console.log(index + ': ' + user)
    
        async.waterfall([
            function(callback) {
                callback(null);
            },
            function(callback) {
                // Skip execution of the rest of waterfall method immediately
                if(index == 1)
                    return callback()
    
                // Some additional code here
    
                callback(null);
            }
        ], function (err, result) {
            console.log(index + ": done")
            cb()
        });
    
    }, function() {
        console.log('ALL done')
    })
    

关于javascript - 如何在 Node.js 中跳过 "async.forEachOf"循环迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31815917/

相关文章:

mysql - ER_ACCESS_DENIED_ERROR : When i try to connect to the database on remote server - Node. js

javascript - 为什么 VS Code 在处理来自 Promise 中的 Reject 的异常时会中断?

node.js - Redis与Postgresql同步(在线用户状态)

javascript - 内部和外部带括号的闭包之间的区别

javascript - Cloudinary 文件上传错误

javascript - Node.js + Express 应用程序中的并发如何工作?

javascript - 干净的代码和嵌套的 promise

C#/ASP.NET异步线程执行

javascript - Node 配置无法解析配置 SyntaxError : Unexpected token

javascript - 如何延迟函数返回,直到发生点击之后