您显然误会了node.js,异步操作和Promise的工作方式。假设长时间运行的异步操作均已使用异步I /
O正确编写,那么您的
requestAsync1(params)或
requestAsync2(params)调用都不会阻塞。这意味着,在等待
Promise.all()调用其
.then()处理程序以表明这两个异步操作均已完成时,node.js完全可以自由运行任何其他事件或传入请求。承诺本身不会阻塞,因此node.js事件系统可以自由处理其他事件。因此,您根本就没有阻塞问题,或者实际上没有阻塞问题,这不是您在此处询问的原因引起的。
要查看您的代码是否实际被阻塞,可以临时添加一个简单的计时器,将其输出到控制台,如下所示:
let startTime;setInterval(function() { if (!startTime) { startTime = Date.now(); } console.log((Date.now() - startTime) / 1000);}, 100)当事件循环未阻塞时,这将每100ms输出一个简单的相对时间戳。您显然不会在生产代码中将其保留在代码中,但是当事件循环被阻止时,向您显示可能会很有用。
我确实在您的问题中包含的代码中看到了一个奇怪的语法问题。这段代码:
someFunctionCall(params, callback){ // do some asyncronous requests. Promise.all([requestAsync1(params),requestAsync2(params)]).then { // do some operations callback(response) // callback given with some data from the promise }}应该这样表示:
someFunctionCall(params, callback){ // do some asyncronous requests. Promise.all([requestAsync1(params),requestAsync2(params)]).then(function(response) { // do some operations callback(response) // callback given with some data from the promise }});但是,更好的设计将是仅返回诺言,而不切换回简单的回调。除了允许调用者使用更灵活的Promise方案外,您还“吃”了异步操作或异步操作中可能发生的错误。建议这样做:
someFunctionCall(params) { // do some asyncronous requests. return Promise.all([requestAsync1(params),requestAsync2(params)]).then(function(results) { // further processing of results // return final resolved value of the promise return somevalue; });}然后,呼叫者会这样使用:
someFunctionCall(params).then(function(result) { // process final result here}).catch(function(err) { // handle error here});


