官术网_书友最值得收藏!

The callback pattern in Node.js

Node.js is single-threaded, just like the web. To handle long running operations it also uses a callback pattern. The callback pattern in Node.js has a a few more details to it and can be described as having the following properties:

  • There is only one function to handle success and error responses
  • The callback is called only once
  • The function is the last parameter of the calling function
  • The callback consists of the parameter's errors and results, in that order, which is also called error-first

Let's now showcase what the calling code looks like, with a callback supplied as the function's last argument:

callAsync('1',2, (error, response) => {
if(error) {
console.error(error);
} else {
console.log('response', response);
// do something with the response
}
})

This piece of code fulfills all the properties laid out by the pattern, namely that the last parameter in the function call is the callback function. Furthermore, the callback function has the error as the first parameter and the response as the second parameter. In addition, the body of the callback function checks whether there is an error first and then, in the absence of an error, deals with the response we get back. 

For reference, let's also look at how callAsync() is implemented:

function callAsync(param, param2, fn) {
setTimeout(() => {
if(param > param2) {
fn(null, 'success');
} else {
fn('error', null);
}
}

The preceding implementation is just a mockup but it does show off two important aspects. One aspect is the time factor the setTimeout() function represents and the fact that the function takes time to complete.

The other aspect is our third parameter, fn() which is invoked differently. We call fn(null, 'success') when everything is alright and fn('error', null) when an error has occurred. The way we invoke fn() is how we communicate success and failure.

主站蜘蛛池模板: 安塞县| 准格尔旗| 随州市| 西乌珠穆沁旗| 祁东县| 浏阳市| 邳州市| 宁海县| 四平市| 乌拉特后旗| 时尚| 阆中市| 桦川县| 米脂县| 滨海县| 昌吉市| 黄陵县| 永定县| 马边| 东山县| 麦盖提县| 云阳县| 新乐市| 建德市| 九龙坡区| 漾濞| 睢宁县| 荥阳市| 大荔县| 东丽区| 南和县| 永嘉县| 黄梅县| 屏南县| 晋中市| 牡丹江市| 黎川县| 宝丰县| 奈曼旗| 柳河县| 兴城市|