`的声明return`目的和`在咕嘟咕嘟任务callback`参考 [英] Purpose of `return` Statement and `callback` Reference in Gulp Task

查看:168
本文介绍了`的声明return`目的和`在咕嘟咕嘟任务callback`参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦一饮而尽运行序列来执行,我给它两个功能(这将只执行其中的一个我有这样的事情:

I was having trouble getting gulp run-sequence to execute both functions that i gave it (it would only execute one of them. I had something like this:

gulp.task('wire', function(){
  gulp.src('./index.html')
    .pipe(wiredep())
    .pipe(gulp.dest('.'));
});

var filesToInject = ['./app/**/*.js', './app/**/*.css'];
gulp.task('inject', function (){
  var target = gulp.src('./index.html');
  var sources = gulp.src(filesToInject, {read: false});
  return target.pipe(inject(sources))
    .pipe(gulp.dest('.'));
});

gulp.task('wire-inject',  function(callback){
    sequence('wire', 'inject', callback);
});

这将只运行任务。只有当我添加在线一收益 gulp.src 任务将它执行。我在这两个功能PARAMS也增加了回调,并在每一>

That would only run the inject task. Only when I added a return before gulp.src in the wire task would it execute. I also added callback in both function params and passed it to the last pipe in both tasks per the documentation.

gulp.task('wire', function(callback){
  gulp.src('./index.html')
    .pipe(wiredep())
    .pipe(gulp.dest('.'), callback);
});

var filesToInject = ['./app/**/*.js', './app/**/*.css'];
gulp.task('inject', function (callback){
  var target = gulp.src('./index.html');
  var sources = gulp.src(filesToInject, {read: false});
  return target.pipe(inject(sources))
    .pipe(gulp.dest('.'), callback);
});

这没扔任何错误,但是如果我把它拿出来它不会改变任何东西。这是什么return语句做那神奇让我彻底顺序运行。这些是什么回调,我只是传递一个参考用括号出来执行这些文档中?我还需要他们,即使他们看似什么都不做?

That did not throw any errors, however it doesn't change anything if I take it out. What does this return statement do that magically makes my sequence run completely. What are these callbacks in the documentation that I just pass a reference to with out parentheses to execute them? Do I still need them even though they seemingly do nothing?

推荐答案

咕嘟咕嘟需要在异步任务完成就知道了。你有3种方式来做到这一点:

Gulp needs to know when an asynchronous task is done. You have 3 ways to do this:


  1. 返回一个事件流,这是当你有返回gulp.src(...)你做什么管(...); 中你的任务的功能。

返回的承诺。

有一个定义,你任务拿一个参数的功能。咕嘟咕嘟会要求有任务的时候已经过去,你应该调用回调函数的。

Have the function that defines you task take a parameter. Gulp will call your function with a callback that you should call when the task is over.

如果你不这样做,那么咕嘟咕嘟不会的知道的该任务是异步的。因此,将考虑尽快完成它的函数返回的任务。这有各种各样的后果。任务完成之前,咕嘟咕嘟可以退出。或者,它可能启动依赖于任务A之前的任务一完成一个任务B。在某些情况下,你可能看不到问题的立即的但你gulpfile变得越来越复杂,运行越来越恶劣的行为更多的机会。 运行序列不是特殊:它需要知道,当任务完成后正确地完成工作。

If you do not do this, then Gulp will not know that the task is asynchronous. So it will consider the task done as soon as its function returns. This has all kinds of consequences. Gulp could exit before a task is done. Or it could start a task B that depends on task A before task A is complete. In some cases, you may not see a problem immediately but as your gulpfile gets more complex, you run more chances of getting bad behavior. run-sequence is not special: it needs to know when a task is complete to do its job correctly.

注意回调在这个code,你在你的问题表明的是无用的:

Note that callback in this code which you show in your question is useless:

return target.pipe(inject(sources))
    .pipe(gulp.dest('.'), callback);

回调将不会被调用。与函数替换它(){执行console.log(你好!);回电话(); } 。你不会看到您好!在控制台上,因为回调不叫。但是,因为你返回流code没有全面工作。一口使用返回到确定该任务已经结束,不回调(其从未无论如何调用)的流

callback won't be called. Replace it with function () { console.log("Hello!"); callback(); }. You won't see Hello! on the console because the callback is not called. However, the code does work overall because you return the stream. Gulp uses the stream you return to determine that the task is over, not the callback (which is never called anyway).

这篇关于`的声明return`目的和`在咕嘟咕嘟任务callback`参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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