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.

gulp seems to hang with async task

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
dewwwaldcommented, Mar 15, 2018

@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);
    });
  }
});
1reaction
nidheeshdascommented, Aug 13, 2014

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gulp [4.0.2] hangs when async function is completed even ...
I have a gulp task that's hanging after completion. Running --verbose on my command tells me that it completed the run, but it...
Read more >
Async Completion - gulp.js
When a stream, promise, event emitter, child process, or observable is returned from a task, the success or error informs gulp whether to...
Read more >
Gulp watch task never terminates, leads to VS hanging waiting ...
VS hangs, tells me it's busy, and is waiting for the node process to be terminated (without actually instructing it to terminate). I...
Read more >
In the name of Gulp - Adventures in brine
Because Gulp tasks are asynchronous (non blocking), the default task has no reason to hang around waiting for all the tasks in the...
Read more >
Gulp - a modern approach to asset pipeline for Rails developers
But after long consideration Gulp seems to be the best candidate: It's simple. Gulp operates on streams - your job is to create...
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