A task in gulp can be defined like so:
gulp.task('foobar', function(callback) { ... });
I'm trying to understand what the callback function is. Where is it defined? Can I pass in some other function as an argument at runtime? What does it do?
These docs indicate that the callback argument is a hint to Orchestrator that the task should be run asynchronously, where the executing the callback indicates that the async task has completed.
With some experimentation it looks like calling the callback with no arguments returns a success state, and calling it with some string throws an error:
gulp.task('foobar', function(callback) {
callback();
});
gulp.task('bazkad', function(callback) {
callback("some string");
});
(aside: how do I place a break between code blocks in StackOverflow markdown?)
$ gulp foobar
[09:59:54] Using gulpfile ~\repos\gulpproj\gulpfile.js
[09:59:54] Starting 'foobar'...
[09:59:54] Finished 'foobar' after 56 μs
$ gulp bazkad
[10:05:49] Using gulpfile ~\repos\gulpproj\gulpfile.js
[10:05:49] Starting 'bazkad'...
[10:05:49] 'bazkad' errored after 55 μs
[10:05:49] Error: some string
at formatError (~\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10)
at Gulp.<anonymous> (~\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15)
at Gulp.emit (events.js:107:17)
at Gulp.Orchestrator._emitTaskDone (~\repos\gulpproj\node_modules\gulp\node_modules\orchestrator\index.js:264:8)
at ~\repos\gulpproj\node_modules\gulp\node_modules\orchestrator\index.js:275:23
at finish (~\repos\gulpproj\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8)
at cb (~\repos\gulpproj\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3)
at Gulp.<anonymous> (~\repos\gulpproj\gulpfile.js:35:5)
at module.exports (~\repos\gulpproj\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (~\repos\gulpproj\node_modules\gulp\node_modules\orchestrator\index.js:273:3)
So, questions I have are:
- Is this the only functionality of the callback, to raise an exception if passed an argument and to complete successfully otherwise, or does it do something else?
- Could I override it with some other function (and would there be any sane reason to do so)?
Maybe my documentation reading skills are failing me (wouldn't be the first time), but I can't seem to find the answers to these questions in the API docs.
Thanks for any help.