Gulp "done" 方法有什么作用?

What does Gulp "done" method do?

只是一个简单的问题来阐明 gulp 任务中的参数 "done" 的作用?

我明白了,这是任务函数中的回调,如下所示。

gulp.task('clean', function(done) {
    // so some stuff
    creategulptask(cleantask(), done);
});

但是为什么要通过呢?

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']);

done 参数被传递到您用来定义任务的回调函数中。

你的任务函数可以"accept a callback"函数参数(通常这个函数参数被命名为done)。执行 done 函数告诉 Gulp "a hint to tell it when the task is done".

Gulp 如果您想订购 系列 相互依赖的任务 ,则需要此提示,如图所示在上面的例子中。 (即任务 two 直到任务 one 调用 cb() 才会开始)本质上,如果您不希望它们停止,它会同时停止来自 运行 的任务。

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