Gulp"完成"方法做了什么?

Nex*_*s23 16 gulp

只是一个简单的问题来澄清"done"gulp任务中的参数是做什么的?

据我所知,这是任务函数中的回调,如下所示.

gulp.task('clean', function(done) {
    // so some stuff
    creategulptask(cleantask(), done);
});
Run Code Online (Sandbox Code Playgroud)

但是通过它的原因是什么?

See*_*eer 23

gulp文档指定类似于以下内容:

var gulp = require('gulp');

// Takes in a callback so the engine knows when it'll be done
// This callback is passed in by Gulp - they are not arguments / parameters
// for your task.
gulp.task('one', function(cb) {
    // Do stuff -- async or otherwise
    // If err is not null and not undefined, then this task will stop, 
    // and note that it failed
    cb(err); 
});

// Identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
    // Task 'one' is done now, this will now run...
});

gulp.task('default', ['one', 'two']);
Run Code Online (Sandbox Code Playgroud)

done参数将传递给用于定义任务的回调函数.

您的任务函数可以"接受回调"函数参数(通常将此函数参数命名done).执行该done功能告诉Gulp"在任务完成时提示告诉它".

如果您想要订购一系列相互依赖的任务, Gulp需要此提示,如上例所示.(即two在任务one调用之前任务不会开始cb())从本质上讲,如果您不希望任务同步,它会阻止任务同时运行.

你可以在这里阅读更多相关信息:https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support

  • 只是好奇,如果我给他的功能有参数,我们如何检查并知道?这在Javascript中怎么可能?这听起来像是反思. (2认同)
  • @HolgerThiemann内部,gulp可以接收传递的函数并检查其`length`属性以查看预期参数的数量. (2认同)