Gulp error: The following tasks did not complete: Did you forget to signal async completion?

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

I have the following gulpfile.js, which I'm executing via the command line "gulp message":

var gulp = require('gulp');  gulp.task('message', function() {   console.log("HTTP Server Started"); }); 

I'm getting the following error message:

[14:14:41] Using gulpfile ~\Documents\node\first\gulpfile.js [14:14:41] Starting 'message'... HTTP Server Started [14:14:41] The following tasks did not complete: message [14:14:41] Did you forget to signal async completion? 

I'm using gulp 4 on a Windows 10 system. Here is the output from gulp --version:

[14:15:15] CLI version 0.4.0 [14:15:15] Local version 4.0.0-alpha.2 

回答1:

Since your task might contain asynchronous code you have to signal gulp when your task has finished executing (= "async completion").

In Gulp 3.x you could get away without doing this. If you didn't explicitly signal async completion gulp would just assume that your task is synchronous and that it is finished as soon as your task function returns. Gulp 4.x is stricter in this regard. You have to explicitly signal task completion.

You can do that in five ways:

1. Return a Stream

This is not really an option if you're only trying to print something, but it's probably the most frequently used async completion mechanism since you're usually working with gulp streams. Here's a (rather contrived) example demonstrating it for your use case:

var print = require('gulp-print');  gulp.task('message', function() {   return gulp.src('package.json')     .pipe(print(function() { return 'HTTP Server Started'; })); }); 

2. Return a Promise

This is a much more fitting mechanism for your use case. Note that most of the time you won't have to create the Promise object yourself, it will usually be provided by a package (e.g. the frequently used del package returns a Promise).

gulp.task('message', function() {    return new Promise(function(resolve, reject) {     console.log("HTTP Server Started");     resolve();   }); }); 

3. Call the callback function

This is probably the easiest way for your use case: gulp automatically passes a callback function to your task as its first argument. Just call that function when you're done:

gulp.task('message', function(done) {   console.log("HTTP Server Started");   done(); }); 

4. Return a child process

This is mostly useful if you have to invoke a command line tool directly because there's no node.js wrapper available. It works for your use case but obviously I wouldn't recommend it (especially since it's not very portable):

var spawn = require('child_process').spawn;  gulp.task('message', function() {   return spawn('echo', ['HTTP', 'Server', 'Started'], { stdio: 'inherit' }); }); 

5. Return a RxJS Observable.

I've never used this mechanism, but if you're using RxJS it might be useful. It's kind of overkill if you just want to print something:

var Observable = require('rx').Observable;  gulp.task('message', function() {   var o = Observable.just('HTTP Server Started');   o.subscribe(function(str) {     console.log(str);   });   return o; }); 


回答2:

Workaround: We need to call the callback functions(Task and Anonymous):

function electronTask(callbackA) {     return gulp.series(myFirstTask, mySeccondTask, (callbackB) =>     {         callbackA();         callbackB();     })();     } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!