gpt4 book ai didi

node.js - Node JS Async Map 是同步还是异步调用

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:15 24 4
gpt4 key购买 nike

当我尝试使用 Node js 异步模块功能(如 waterfall 、mapSeries 等)时,大多数功能本质上都是异步的。但看起来异步映射不是异步的。例如,我尝试了这个:

var async = require('async');

console.dir('This is the begining of the program');
async.mapSeries([6,7,8,9], justAddOne, asyncMapCompleted);
function justAddOne(number, callback) {
callback(null, ++number);
}

function asyncMapCompleted(error, result) {
console.log("map completed. Error: ", error, " result: ", result);
}

async.map([1,2,3,4,5], justAddOne, asyncMapCompleted);

console.dir('This is the end of the program');

我得到以下输出:

'This is the begining of the program'
map completed. Error: null result: [ 2, 3, 4, 5, 6 ]
'This is the end of the program'
map completed. Error: null result: [ 7, 8, 9, 10 ]

从输出中我们可以看到,当我们调用 async.map 函数时,执行线程会停止直到完成(在 async map 之后我们得到输出“'This is the end of the program'”)。

但是,我们在程序开始时调用了异步mapSeries,它不会停止执行,而是在“结束程序”消息后打印结果。

那么,async.map是同步调用?我尝试查看文档但找不到它。

最佳答案

根据消息来源,async.eachOf (从 map 调用)没有执行任何使其异步的操作,其中 async.eachOfSeries (从mapSeries调用)在内部使用async.setImmediate

关于node.js - Node JS Async Map 是同步还是异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603338/

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