question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

task not waiting for dependency to finish

See original GitHub issue

Shouldn’t this work?

var gulp = require('gulp');
var del = require('del');

...

gulp.task('clean', function(callback) {
  del([ site.build + '/**' ], callback);
});

gulp.task('build', [ 'clean' ], function() {
  return gulp.start('html', 'less', 'js', 'png', 'jpg');
});

gulp.task('deploy', [ 'build' ], function() {
 ...
});

Unfortunately deploy runs before build finished.

gulp.task('clean', function(callback) {
  del([ site.build + '/**' ], callback);
});

gulp.task('build', [ 'clean', 'html', 'less', 'js', 'png', 'jpg' ], function() {
});

gulp.task('deploy', [ 'build' ], function() {
 ...
});

The above does not work either as clean is run in parallel.

So what’s the suggested way of doing this?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
thomashigginbothamcommented, Aug 23, 2016

Even with run-sequence, I was having the same problem. Here’s how I worked around it:

gulp.task('build', function(callback) {
  gulp.src('abc.xyz')
    .pipe(someTask())
    .pipe(gulp.dest('dist'))
    .on('end', callback);
});

gulp.task('deploy', [ 'build' ], function() {
  /* The .on('end', callback) above ensures that the "build" task has fully completed. It's
     now safe to use "dist/abc.xyz". */
});
2reactions
leonliaocommented, Dec 6, 2016

Met same issue here. Let’s say there are 2 tasks, First and Second. Second runs after First. The First task generates some files, which are to be read by the Second task. Using dependency doesn’t make sure the Second task can find the files generated.

I have to explicitly using the done callback on the pipeline to let Second only starts after First truly done.

//This approach works!
gulp.task('First', function(done)) {
   var subFolders = fs.readdirSync(somefolder)...
   var tasksForFolders = subFolders.map(function(folder) {
       return gulp.src('folder/**/*').sthtogeneratefiles();
   });
   tasksForFolders[tasksForFolders.length-1].on('end',done);
   return tasksForFolders;
}

gulp.task('Second', ['First'],function() {
    return gulp.src('generatedfolders/**/*').doth();
}

Without the done trick, the Second never finds the files generated by First. Below shows what I tried, the Second task can find the files generated by calling gulp First by hand, and then calling gulp Second subsequently.

//This is the WRONG approach, just for demonstration!!
gulp.task('First', function()) {
   var subFolders = fs.readdirSync(somefolder)...
   var tasksForFolders = subFolders.map(function(folder) {
       return gulp.src('folder/**/*').sthtogeneratefiles();
   });
   return tasksForFolders;
}

gulp.task('Second', function() {
    return gulp.src('generatedfolders/**/*').doth();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Waiting for task to be finished - Stack Overflow
In the above code, I have written Task1.wait() which waits for task1 to be finished but not for all the tasks created in...
Read more >
Just because you stopped waiting for it, doesn't mean the Task ...
Without special handling, a Task always run to completion. Let's start by looking at what happens to the "source" Task when you use...
Read more >
How to describe the "waiting time" as someone else has yet to ...
Try 'dependency'. – Tushar Raj. Jul 1, 2016 at 6:34.
Read more >
Finish-to-finish dependency explained by example
Finish-to-finish (FF) means that one task can only be completed after the other task has been completed.
Read more >
Wait for the execution of a task to complete before running ...
All of those printlns are executing at “configuration time” instead of “execution time”, so task dependencies aren't taken into account at that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found