gulp: gulp seems to hang with async task

hi,

I might be missing something here, but let’s say I define a task as follows.

gulp.task('aaa', function(cb){
  http.get('http://www.google.com/index.html', function(res){
    gutil.log('in result');
    cb();
  });
});

If I run this task from default and then invoke gulp from the command line I get

prompt$ gulp
[gulp] Running 'default'...
[gulp] Running 'aaa'...
[gulp] Finished 'default' in 4.9 ms
[gulp] in result
[gulp] Finished 'aaa' in 134 ms

But the process hangs there and I don’t get back to the prompt.

However, If redefine the task as follows, and run it

gulp.task('aaa', function(cb){
  setTimeout(cb, 2000);
});
prompt$ gulp
[gulp] Running 'default'...
[gulp] Running 'aaa'...
[gulp] Finished 'default' in 833 μs
[gulp] Finished 'aaa' in 2 s
prompt$

This works as expected and the process terminates.

Not sure what I am missing, but any pointers would be great.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 6
  • Comments: 19 (2 by maintainers)

Commits related to this issue

Most upvoted comments

@nidheeshdas nice solution.

You can monkey patch watch for cleaner code.

const watch = gulp.watch;
gulp.watch = function (...args) {
  watching = true;
  return watch(...args);
};

gulp.on('stop', function () {
  if (!watching) {
    process.nextTick(function () {
      process.exit(0);
    });
  }
});

here’s a solution:

var isWatching = false;

gulp.task('watch', [....], function() {
  isWatching = true;
 .... ...
});

gulp.on('stop', function() {
    if (!isWatching) {
        process.nextTick(function() {
            process.exit(0);
        });
    }
});

process.exit happens only if you are not watching.

This is curious. Can you post your entire gulpfile.js for both scenarios?