简体   繁体   English

nodejs - async.each 函数在每次迭代中进行异步操作

[英]nodejs - async.each function with async operation on each iteration

I have a question about async.each behavior.我有一个关于 async.each 行为的问题。 consider the code:考虑代码:

const examples = [1,2];

const asyncTask = (index) => {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(`Inside setTimeout-${index}`);
            resolve(true);
        }, 1500)
    });
}

function testingAsyncEach(callback){
    async.each(examples, (example, innerCallback) => {
        asyncTask(example).then(isDone => {
            console.log(`isDone: ${isDone}`);
            innerCallback(null);
        });
    }, err => {
        console.log('done testingAsyncEach');
        return callback()
    })
}

testingAsyncEach(() => {console.log('final callback testingAsyncEach');})

a simple code using the "async" module in nodejs, using the array [1,2] and on each item in the array executing the function "asyncTask" which returns a new promise which gets resolve after a timeout of 1.5 seconds.使用 nodejs 中的“async”模块的简单代码,使用数组 [1,2] 并在数组中的每个项目上执行函数“asyncTask”,该函数返回一个新的承诺,该承诺在 1.5 秒超时后得到解决。

in this scenario the output of the program is:在这种情况下,程序的输出是:

Inside setTimeout-1
isDone: true
Inside setTimeout-2
isDone: true
done testingAsyncEach
final callback testingAsyncEach

but when I changed the "testingAsyncEach" function to use the "await" syntax:但是当我将“testingAsyncEach”函数更改为使用“await”语法时:

function testingAsyncEach(callback){
    async.each(examples, async (example, innerCallback) => {
        const isDone = await asyncTask(example);
        console.log(`isDone: ${isDone}`);
        innerCallback(null);
    }, err => {
        console.log('done testingAsyncEach');
        return callback()
    })
}

the async.each is not waiting for the "asyncTask" to end. async.each 不会等待“asyncTask”结束。 output:输出:

Inside setTimeout-1
isDone: true
done testingAsyncEach
final callback testingAsyncEach 
Inside setTimeout-2
isDone: true

Can you please help me understand why using the "await" instead of the "then" change the behavior?你能帮我理解为什么使用“await”而不是“then”来改变行为吗? how can I still use the "await" syntax and get the proper output?我怎样才能仍然使用“await”语法并获得正确的输出? Thanks!谢谢!

According to the documentation of the async module, if you pass an async function, it won't pass the innerCallback to your function.根据 async 模块的文档,如果您传递async函数,它不会将innerCallback传递给您的函数。 I believe that your innerCallback(null) call therefore errored, making the async.each return early.我相信您的innerCallback(null)调用因此出错,使async.each返回。 (the 2nd example is already "launched" though, so that still happens afterwards) (虽然第二个例子已经“启动”了,所以之后仍然会发生)

Check if err is set to an error, and if so, log it.检查err是否设置为错误,如果是,请记录。

Should that be the issue, removing the innerCallback argument and call should solve it.如果这是问题,删除innerCallback参数和调用应该可以解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM