0

I am using the async library and am using the async.waterfall function whose signature is async.waterfall([function1,function2,function3],function(err,result){}). Now i understand that the results of each function is passed on to the next and the final callback function is executed in case of some error in between or when the execution is over.

What i do not understand is that each function in the array of functions passed takes a callback as an argument and the callback is executed in each function with the result of that function. But i do not understand where that callback function is defined.

Below is a sample piece of code

 async.waterfall([ function getUserAvatar(callback) { console.log("Callback function is " + callback)//print the callback definition function github.search.users({ q: 'airbnb' }, function(err, res) { if (err) { callback(err, null); return; } var avatarUrl = res.items[0].avatar_url; callback(null, avatarUrl); }); }, function wrapAvatarInHtml(avatarUrl, callback) { var avatarWithHtml = '<img src="' + avatarUrl + '"/>'; callback(null, avatarWithHtml); } ], function(err, result) { if (err) { console.error(err); return; } console.log(result); }) 

When i execute the above code, the line where i do console.log("Callback function is " + callback) it prints the below definition.

function () { if (fn === null) throw new Error("Callback was already called."); var callFn = fn; fn = null; callFn.apply(this, arguments); } 

Now i do not understand where it is getting this definition. I searched the async library also for this but i could not find it. Can anyone explain this.

5
  • It is the implementation of waterfall that calls your functions with its own callback function. It needs to do that so it gets notified of the asynchronous success/error event and can then take action within its own callback to trigger the next function in the waterfall, etc. Commented Mar 27, 2019 at 9:51
  • @trincot, I misread the question, I thought OP was asking for definition of waterfall, not waterfall callback :p Commented Mar 27, 2019 at 9:52
  • 1
    Sorry, I got it wrong again, what you're actually seeing is the function returned by the onlyOnce function - github.com/caolan/async/blob/master/dist/async.js#L298 Commented Mar 27, 2019 at 9:54
  • @trincot : i agree with what you are saying but when we install the async library using npm -i async wwe should be getting that definition along with it. But unfortunately i could not find that definition. Commented Mar 27, 2019 at 9:55
  • @JaromandaX: Thanks for your answer. Exactly what i was looking for and it was driving me crazy .... I was using Visual Studio Code to search that function in the whole directory .... apparently that does not do a good job at searching strings in the whole folder directory. I have found it now. Thanks Commented Mar 27, 2019 at 10:00

2 Answers 2

0

If you go through the code in this link. The next() defined is the callback being called. It is implicit, you don't have to define and is because even the tasks you are passing to async waterfall is not being called by you, but by async.waterfall()

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the library code of async waterfall. You will get the fair idea about how it works

exports.default = function (tasks, callback) { callback = (0, _once2.default)(callback || _noop2.default); if (!(0, _isArray2.default)(tasks)) return callback(new Error('First argument to waterfall must be an array of functions')); if (!tasks.length) return callback(); var taskIndex = 0; function nextTask(args) { var task = (0, _wrapAsync2.default)(tasks[taskIndex++]); args.push((0, _onlyOnce2.default)(next)); task.apply(null, args); } function next(err /*, ...args*/) { if (err || taskIndex === tasks.length) { return callback.apply(null, arguments); } nextTask((0, _slice2.default)(arguments, 1)); } nextTask([]); }; 

For more information, please install the async library and look into the node modules folder. You will get the complete idea of async library.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.