最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Apr 18, 2015 at 20:38 Bryce JohnsonBryce Johnson 6,9017 gold badges46 silver badges52 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

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);
  });
};

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;
};
发布评论

评论列表(0)

  1. 暂无评论