Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I promise-ify a one-off usage of gulp in my application?

As part of a small program I'm writing, I would like to use gulp to convert a large set of a files to markdown. This is not part of a build step separate from the program. It's a part of the program. So I'm not using a gulpfile to handle this.

The problem is, since it's async, I want to use a promise which will alert me when the gulp task is finished.

Something like this would be ideal:

io.convertSrc = function() {
  var def = q.defer();

  gulp.src(src + '/*.md')
    .pipe(marked({}))
    .pipe(gulp.dest(dist), function() {
      def.resolve('We are done!');
    });

    return def.promise;
}

But pipe doesn't take a callback. How could I handle this? Thanks for your help, I'm somewhat new to gulp.

like image 783
Bryce Johnson Avatar asked Apr 18 '15 20:04

Bryce Johnson


People also ask

How to define a gulp task?

Each gulp task is an asynchronous JavaScript function - a function that accepts an error-first callback or returns a stream, promise, event emitter, child process, or observable (more on that later).

How do you signal async completion gulp?

To indicate to gulp that an error occurred in a task using an error-first callback, call it with an Error as the only argument. However, you'll often pass this callback to another API instead of calling it yourself.

What is CB () in gulp?

I'd say that sometimes giving cb a more intuitive name (e.g. "operationComplete") can help with confusion, which I think might be the use case you're referring to in gulp. – Joshua. Aug 3, 2015 at 7:33. 2. so cb is just short for callback, and can be replaced with any other variable names?


2 Answers

Everything in gulp is a stream, so you can just listen for the end and error events.

io.convertSrc = function() {
  var def = q.defer();
  gulp.src(src + '/*.md')
    .pipe(marked({}))
    .pipe(gulp.dest(dist))
    .on('end', function() {
      def.resolve();
    })
    .on('error', def.reject);
  return def.promise;
}

As an aside, Q 1.0 is no longer developed (aside from a few fixes here and there) and will be wholly incompatible with Q 2.0; I'd recommend Bluebird as an alternative.

Also worth mentioning that NodeJS 0.12 onwards has ES6 promises built into it (no --harmony flag necessary) so if you're not looking for backwards compatibility you can just use them instead..

io.convertSrc = function() {
  return new Promise(function(resolve, reject) {
    gulp.src(src + '/*.md')
      .pipe(marked({}))
      .pipe(gulp.dest(dist))
      .on('end', resolve)
      .on('error', reject);
  });
};
like image 141
Dan Avatar answered Oct 05 '22 22:10

Dan


Since the Gulp task is a stream, you can listen for its events:

io.convertSrc = function() {
  var def = q.defer();

  var stream = gulp.src(src + '/*.md')
    .pipe(marked({}))
    .pipe(gulp.dest(dist));

  stream.on('end', function() {
    def.resolve();
  });

  stream.on('error', function(err) {
    def.reject(err);
  });

  return def.promise;
};
like image 34
William Gaul Avatar answered Oct 05 '22 22:10

William Gaul