吞吐错误:以下任务未完成:您是否忘记发出异步完成信号? [英] Gulp error: The following tasks did not complete: Did you forget to signal async completion?

查看:750
本文介绍了吞吐错误:以下任务未完成:您是否忘记发出异步完成信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下gulpfile.js,我通过命令行gulp message执行:

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

我收到以下错误消息:

  [14:14:41]使用gulpfile〜\Documents\\\
ode\first\gulpfile.js
[14:14:41]正在启动'消息'...
HTTP服务器已启动
[14:14:41]以下任务未完成:message
[14:14:41]您忘记了发出异步完成信号?

我在Windows 10系统上使用gulp 4。这是gulp的输出--version:

  [14:15:15] CLI版本0.4.0 
[14:15:15]本地版本4.0.0-alpha.2


解决方案因为你的任务可能包含异步代码,所以当你的任务完成执行时(=异步完成),你必须发出信号。



在Gulp 3.x中,您可以不用这样做。如果您没有明确指出异步完成吞吐量会假定您的任务是同步的,并且只要您的任务函数返回就立即完成。 Gulp 4.x在这方面更严格。您必须明确指示任务完成。



您可以在五种方式

1。返回一个



如果您'只是试图打印一些东西,但它可能是最常用的异步完成机制,因为你通常使用gulp流。下面是一个(颇为人为的)示例,演示了它的用例:

  var print = require('gulp-print'); 

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



2。返回 Promise



这对您的用例来说更合适。请注意,大多数情况下,您不需要自己创建 Promise 对象,它通常由包提供(例如,经常使用的 del 包会返回一个 Promise ) 。

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



3。调用回调函数



这可能是最简单的用例:gulp自动将回调函数作为第一个参数传递给您的任务。只需在完成后调用该函数:

pre $ g $ p $ gulp.task('message',function(done){
console.log(HTTP Server Started);
done();
});



4。返回一个子进程



如果您有直接调用命令行工具,因为没有可用的node.js包装器。它适用于你的用例,但显然我不会推荐它(特别是因为它不是很便携):

  var spawn =要求( 'child_process')产卵。; 
$ b $ gulp.task('message',function(){
return spawn('echo',['HTTP','Server','Started'],{stdio:'inherit '});
});



5。返回 RxJS Observable



我从来没有使用这种机制,但如果您使用RxJS,它可能会有用。如果你只是想打印一些东西,这有点矫枉过正:

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


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

解决方案

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;
});

这篇关于吞吐错误:以下任务未完成:您是否忘记发出异步完成信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆