The Node library has many ways to handle asynchronous functions. The most common mode is error-first callbacks . In addition, there are streams , promise() , event emitters , child processes , observables . gulp task standardizes all these types of asynchronous functions.

Task completion notification

stream , promise , event emitter , child process or observable are returned from the task, the success or error value will inform gulp whether to continue or end. If there is an error in the task, gulp will immediately end the execution and display the error.

When using series() combine multiple tasks, an error in any one task will cause the entire task combination to end, and no further tasks will be executed. When using parallel() combine multiple tasks, an error in one task will end the entire task combination, but other parallel tasks may or may not be completed.

Return stream

const { src, dest } = require('gulp');

function streamTask() {
  return src('*.js')
    .pipe(dest('output'));
}

exports.default = streamTask;

Return promise

function promiseTask() {
  return Promise.resolve('the value is ignored');
}

exports.default = promiseTask;

Return event emiter

const { EventEmitter } = require('events');

function eventEmitterTask() {
  const emitter = new EventEmitter();
  // Emit has to happen async otherwise gulp isn't listening yet
  setTimeout(() => emitter.emit('finish'), 250);
  return emitter;
}

exports.default = eventEmitterTask;

Return child process

const { exec } = require('child_process');

function childProcessTask() {
  return exec('date');
}

exports.default = childProcessTask;

Return observable

const { Observable } = require('rxjs');

function observableTask() {
  return Observable.of(1, 2, 3);
}

exports.default = observableTask;

Return callback

If the task does not return anything, you must use callback to indicate that the task is complete, and the callback will be passed to the task cb()

function callbackTask(cb) {
  cb();
}

exports.default = callbackTask;

To gulp of the error in the task callback , please use Error as the only parameter of callback

function callbackError(cb) {
  // `cb()` should be called by some async work
  cb(new Error('kaboom'));
}

exports.default = callbackError;

However, you usually callback function to another API instead of calling it yourself.

const fs = require('fs');

function passingCallback(cb) {
  fs.access('gulpfile.js', cb);
}

exports.default = passingCallback;

Use async/await

If you don't use the methods provided above, we can also define the task as a async function, which will use promise to package your task. This will allow you to use await process promise and use other synchronization codes.

Example:
const fs = require('fs');

async function asyncAwaitTask() {
  const { version } = fs.readFileSync('package.json');
  console.log(version);
  await Promise.resolve('some result');
}

exports.default = asyncAwaitTask;

知否
221 声望177 粉丝

Skrike while the iron is hot.


« 上一篇
Gulp 导出任务
下一篇 »
Gulp 处理文件

引用和评论

0 条评论