ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 调用Node.js的async.parallel()同步吗?

2019-10-02 06:36:34  阅读:196  来源: 互联网

标签:async-js javascript node-js asynchronous


我正在看看Node.js的异步模块来解决问题.我已经实施了一个小测试:

var async = require("async");

function print(val) {
    console.log(val);
}

async.parallel([
    function(cb){ print(1); cb(null); },
    function(cb){ print(2); cb(null); },
    function(cb){ print(3); cb(null); },
    function(cb){ print(4); cb(null); },
    function(cb){ print(5); cb(null); }
],
    function(err) {
        if ( err ) {
            console.error(err);
            return;
        }
        console.log("Done!");
    }
);

console.log("Trulu");

我可以确定在调用async.parallel完成之前永远不会调用Trulu日志调用吗?换句话说,在调用Trulu日志之前调用的所有函数和最终回调是肯定的吗?

解决方法:

Can I be sure that the Trulu log call will never be called before the call to async.parallel is finished?

电话本身,是的.

In other words, are all the functions

我想是的,虽然文档没有提到它.但是我希望不会有任何变化,因为这可能会破坏现有代码.

and the final callback called before the Trulu log is called for sure?

不.你不知道这些功能,当它们是异步的时候,最终的回调将在Trulu之后执行.如果所有函数都是同步的,则无论如何都不应该使用async.实际上,虽然async.js确实使用setImmediate internally所以一些回调(遗憾的是很难识别)将是“异步”,但维护者stated

Yes, making functions asynchronous is left up to the developer

如果要确保始终以异步方式调用回调(在Trulu之后),即使函数是同步的,也可以考虑使用A+ compliant Promises.

标签:async-js,javascript,node-js,asynchronous
来源: https://codeday.me/bug/20191002/1841384.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有