node.js - What is cb() in Node? -


where people getting cb() from, node thing or vanilla js thing?

for example:

managing node.js callback hell promises, generators , other approaches

they're using cb() guess callback , return error or value or both in cases depending on callback function sig is?

cb in context you're describing how vanilla callback function passed (typically) asynchronous function, common pattern in node.js (it's labelled next, can call bananas if desire - it's argument).

typically first argument error object (often false - if went planned) , subsequent arguments data of form.

for example:

function myasyncfunction(arg1, arg2, cb) {     // async things     cb(false, { data: 123 }); } 

then using function:

myasyncfunction(10, 99, function oncomplete(error, data) {     if (!error) {         // hooray, went planned     } else {         // disaster - retry / respond error etc     } }); 

promises alternative design pattern return promise object myasyncfunction

for example:

function myasyncfunction2(arg1, arg2) {     return new promise(function resolution(resolve, reject, {         // async things         resolve({ data: 123 });     }); } 

then using function:

myasyncfunction2(10, 99)     .then(function onsuccess(data) {         // success - send 200 code etc     })     .catch(function onerror(error) {         // oh noes - 500     }); 

they're same thing, written differently. promises aren't supported widely in native form, if put through transpiler (i'd recommend babel) during build step should perform reliably enough in browser too.

callbacks work in browser no shimming / transpilation.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -