Nodejs 异步编程 - 为什么需要 "async" 模块?什么是 "Callback Hell" / "Pyramid of Doom"?
Nodejs Asynchronous Programming - why there is "async" module required? What is "Callback Hell" / "Pyramid of Doom"?
NodeJS
的最大特点之一是它 asynchronous
从我正在阅读的内容中开箱即用,但是作为 NodeJS
的初学者,这有点令人困惑为什么如果已经在本机处理,是否存在像 async
这样的模块?
https://www.npmjs.com/package/async
我认为这是有充分理由的,但对我来说并不明显。是处理callback hell
还是Pyramid of Doom
.
阅读说明:
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
它没有 "provide" 异步函数,它提供了处理异步的函数 javascript。
注意:javascript是不是全部异步,只是异步的部分是异步的。
换句话说
async doesn't make nodejs asynchronous, it makes using asynchronous code simpler through its sugar coated goodness
When you use asynchronous programming in NodeJS
, you may end
up with Callback Hell
or Pyramid of Doom
when you have more number of asynchronous functions to be called one after another one as below.
Callback - 一旦你的第一个函数被异步执行,你的主线程应该会收到通知。为此,您将一个函数作为 callback
传递,该函数将在异步操作完成后触发。
当链中或大循环中有更多异步函数时,您可能必须传递相同数量的回调以找出每个操作的完成情况,最后一个执行其他操作,例如返回响应等
当您使用更多回调对它们进行编码时,manage/maintain 变得非常困难并且缺乏更好的可读性,如下所示。
getData(function(a){
getMoreData(a, function(b){
getMoreData(b, function(c){
getMoreData(c, function(d){
getMoreData(d, function(e){
...
});
});
});
});
});
To get rid of these disadvantages and for better readability and
maintenance, we may go with other modules such as async
, bluebird
etc. You can choose whatever you like which seems to be better for you
in terms of understanding and satisfying all the requirements without
making things too complex.
无论如何,这完全取决于您使用 callback hell
或其他模块。
要获得更深入的见解,
https://strongloop.com/strongblog/node-js-callback-hell-promises-generators/
NodeJS
的最大特点之一是它 asynchronous
从我正在阅读的内容中开箱即用,但是作为 NodeJS
的初学者,这有点令人困惑为什么如果已经在本机处理,是否存在像 async
这样的模块?
https://www.npmjs.com/package/async
我认为这是有充分理由的,但对我来说并不明显。是处理callback hell
还是Pyramid of Doom
.
阅读说明:
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
它没有 "provide" 异步函数,它提供了处理异步的函数 javascript。
注意:javascript是不是全部异步,只是异步的部分是异步的。
换句话说
async doesn't make nodejs asynchronous, it makes using asynchronous code simpler through its sugar coated goodness
When you use asynchronous programming in
NodeJS
, you may end up withCallback Hell
orPyramid of Doom
when you have more number of asynchronous functions to be called one after another one as below.
Callback - 一旦你的第一个函数被异步执行,你的主线程应该会收到通知。为此,您将一个函数作为 callback
传递,该函数将在异步操作完成后触发。
当链中或大循环中有更多异步函数时,您可能必须传递相同数量的回调以找出每个操作的完成情况,最后一个执行其他操作,例如返回响应等
当您使用更多回调对它们进行编码时,manage/maintain 变得非常困难并且缺乏更好的可读性,如下所示。
getData(function(a){
getMoreData(a, function(b){
getMoreData(b, function(c){
getMoreData(c, function(d){
getMoreData(d, function(e){
...
});
});
});
});
});
To get rid of these disadvantages and for better readability and maintenance, we may go with other modules such as
async
,bluebird
etc. You can choose whatever you like which seems to be better for you in terms of understanding and satisfying all the requirements without making things too complex.
无论如何,这完全取决于您使用 callback hell
或其他模块。
要获得更深入的见解,
https://strongloop.com/strongblog/node-js-callback-hell-promises-generators/