只是一個簡單的問題來澄清吞嚥任務中參數"done"
的作用是什麼?Gulp「完成」方法做什麼?
我明白了,這是任務函數的回調函數,如下所示。
gulp.task('clean', function(done) {
// so some stuff
creategulptask(cleantask(), done);
});
但是通過它的原因是什麼?
只是一個簡單的問題來澄清吞嚥任務中參數"done"
的作用是什麼?Gulp「完成」方法做什麼?
我明白了,這是任務函數的回調函數,如下所示。
gulp.task('clean', function(done) {
// so some stuff
creategulptask(cleantask(), done);
});
但是通過它的原因是什麼?
吞氣文檔指定類似下面的東西:
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
)。執行done
函數告訴Gulp「任務完成時提示它」。如果您想訂購的是互相依賴,如在上面的例子中的任務一系列
咕嘟咕嘟需要這個提示。 (即任務two
將不會開始,直到任務one
調用cb()
)實質上,如果您不想讓它們同時運行,那麼它將停止併發運行任務。
您可以在這裏閱讀更多關於此:https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support
done
參數不是回調,匿名function
是回調。 done
只是一個參數,你可以傳遞到你的回調方法出於任何原因。
一飲而盡任務是typically defined as:
gulp.task('somename', function() {
// Do stuff
});
你可以定義任務中執行什麼樣的代碼的功能。如果此代碼是依賴於任何參數,你可以將它們作爲函數的參數:
gulp.task('birthdayTask', function(name, dateOfBirth) {
doFancyStuff(name, dateOfBirth);
});
在你的情況,done
可能是因爲cleantask()
方法完成被儘快執行另一個回調。因此,當cleantask完成時,它將充當某種通知機制。但是,這不能從您的代碼中派生出來,因爲您沒有提供cleantask()
函數的代碼,所以只需在此猜測。
明白了。它與ajax中的異步機制相同嗎? – Nexus23
這是不正確的。你不能像這樣將參數傳遞給gulp任務,你必須使用可用的許多參數處理器之一。看到我的答案。 – Seer
至少使用[gulp-param](https://github.com/stoeffel/gulp-param)插件可以將參數傳遞給gulp任務。我一直在做。儘管如此,你也許是對的。我很抱歉。 – user1438038
很好解釋。謝謝@Seer – Nexus23
只是好奇,如果我給他的函數有一個參數或者沒有參數,那麼gulp如何檢查和知道?這在Javascript中如何實現?這聽起來像反思。 –
如果您需要在任務內運行異步進程,而您希望任務等待完成,那麼在返回之前,回調特別有用。否則,只需返回流就足夠了。另請參閱https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support – grtjn