gulp:gulp任务回调函数在哪里定义的?
gulp: where is the gulp task callback function defined?
gulp中的任务可以这样定义:
gulp.task('foobar', function(callback) { ... });
我正在尝试了解回调函数是什么。它在哪里定义?我可以在 运行 时间传入一些其他函数作为参数吗?它有什么作用?
These docs 表示回调参数是对 Orchestrator 任务应该 运行 异步的提示,其中执行回调表示异步任务已完成。
通过一些实验,似乎调用不带参数的回调 returns 是成功状态,而使用一些字符串调用它会引发错误:
gulp.task('foobar', function(callback) {
callback();
});
gulp.task('bazkad', function(callback) {
callback("some string");
});
(旁白:如何在 Whosebug 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)
所以,我的问题是:
- 这是回调的唯一功能吗?如果传递参数则引发异常,否则成功完成,或者它会做其他事情吗?
- 我可以用其他功能覆盖它吗(这样做有什么合理的理由吗)?
也许我的文档阅读技巧让我失望了(这不是第一次了),但我似乎无法在 API 文档中找到这些问题的答案。
感谢您的帮助。
回调函数来自Orchestrator(或者Gulp4中的新的承办者),实际上无非就是告诉任务系统你的任务是"done".这就是为什么他们将其更改为
gulp.task('something', function(done) { ... });
在即将发布的文档中使这一点更清楚。
为什么需要回调?通常,您 return 在定义任务时使用流:
gulp.task('goodstuff', function() {
return gulp.src('./app/**/*.*')
.pipe(someotherstuff())
.pipe(gulp.dest('./dist');
});
通过return流,任务系统能够计划这些流的执行。但有时,尤其是当您处于回调地狱或调用某些无流插件时,您无法 return 流。这就是回调的用途。让任务系统知道您已完成并继续执行链中的下一个调用。
针对您的问题:
Is this the only functionality of the callback, to raise an exception if passed an argument and to complete successfully otherwise?
没有,唯一的功能是让任务系统知道你的任务已经完成。
Is there anything else that it does?
没有
Could I override it with some other function (and would there be any sane reason to do so)?
没有也没有
Is it possible to pass any other arguments to a gulp task function?
不,但你为什么要这样做?您在服务中拥有 JS 文件的完整范围,只需将您的论点放在周围的某个地方。
gulp中的任务可以这样定义:
gulp.task('foobar', function(callback) { ... });
我正在尝试了解回调函数是什么。它在哪里定义?我可以在 运行 时间传入一些其他函数作为参数吗?它有什么作用?
These docs 表示回调参数是对 Orchestrator 任务应该 运行 异步的提示,其中执行回调表示异步任务已完成。
通过一些实验,似乎调用不带参数的回调 returns 是成功状态,而使用一些字符串调用它会引发错误:
gulp.task('foobar', function(callback) {
callback();
});
gulp.task('bazkad', function(callback) {
callback("some string");
});
(旁白:如何在 Whosebug 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)
所以,我的问题是:
- 这是回调的唯一功能吗?如果传递参数则引发异常,否则成功完成,或者它会做其他事情吗?
- 我可以用其他功能覆盖它吗(这样做有什么合理的理由吗)?
也许我的文档阅读技巧让我失望了(这不是第一次了),但我似乎无法在 API 文档中找到这些问题的答案。
感谢您的帮助。
回调函数来自Orchestrator(或者Gulp4中的新的承办者),实际上无非就是告诉任务系统你的任务是"done".这就是为什么他们将其更改为
gulp.task('something', function(done) { ... });
在即将发布的文档中使这一点更清楚。
为什么需要回调?通常,您 return 在定义任务时使用流:
gulp.task('goodstuff', function() {
return gulp.src('./app/**/*.*')
.pipe(someotherstuff())
.pipe(gulp.dest('./dist');
});
通过return流,任务系统能够计划这些流的执行。但有时,尤其是当您处于回调地狱或调用某些无流插件时,您无法 return 流。这就是回调的用途。让任务系统知道您已完成并继续执行链中的下一个调用。
针对您的问题:
Is this the only functionality of the callback, to raise an exception if passed an argument and to complete successfully otherwise?
没有,唯一的功能是让任务系统知道你的任务已经完成。
Is there anything else that it does?
没有
Could I override it with some other function (and would there be any sane reason to do so)?
没有也没有
Is it possible to pass any other arguments to a gulp task function?
不,但你为什么要这样做?您在服务中拥有 JS 文件的完整范围,只需将您的论点放在周围的某个地方。