Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will this gulp command wait for a dependent task?

I have the following gulp configuration:

gulp.task('bar', function() {
    return gulp.src(files, { base: './' })
        .pipe(gulp.dest('dist'))
});


gulp.task('foo', ['bar'], function() {
    // do something...
});

Will foo wait for bar to complete before it runs?

like image 587
Ben Aston Avatar asked Dec 06 '25 15:12

Ben Aston


2 Answers

According to the docs here:

An array of tasks to be executed and completed before your task will run.

So yes, it will complete before it runs the task.

Also, be aware of the following note. If your tasks are firing off background tasks, and not returning correctly, gulp may incorrectly think that your task is complete.

Note: Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints: take in a callback or return a promise or event stream.

like image 159
ugh StackExchange Avatar answered Dec 09 '25 04:12

ugh StackExchange


In addition to the comments and the other answer, I just want to add that you need to return a stream, a promise, or call the task function callback parameter.

Gulp uses Orchestrator for its tasks.

Returning a stream (most common case, similar with a promise)

gulp.task('somename', function(done) {
    return gulp.src(res)
      .pipe(minify())
      .pipe(gulp.dest('build'));
});

Using the callback (when returning is not an option)

gulp.task('somename', function(done) {

  // async function which does not return a stream like other gulp functions
  getFilesAsync(function(err, res) {
    // pass any errors to the callback
    if (err) return done(err);

    var stream = gulp.src(res)
      .pipe(minify())
      .pipe(gulp.dest('build'))
      .on('end', done); // use the callback when it's done
  });
});

If you don't do one of these, gulp won't know when the task is finished and will start the next task immediately.

like image 26
Emile Bergeron Avatar answered Dec 09 '25 03:12

Emile Bergeron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!