parallel()#

将任务函数和/或组合操作组合成将同时执行的较大操作。使用 series()parallel() 的组合操作的嵌套深度没有限制。

¥Combines task functions and/or composed operations into larger operations that will be executed simultaneously. There are no imposed limits on the nesting depth of composed operations using series() and parallel().

用法#

¥Usage

const { parallel } = require('gulp');
function javascript(cb) {
// body omitted
cb();
}
function css(cb) {
// body omitted
cb();
}
exports.build = parallel(javascript, css);

签名#

¥Signature

parallel(...tasks)

参数#

¥Parameters

参数typenote
任务
(必填)
函数
字符串
任意数量的任务函数可以作为单独的参数传递。如果你之前注册过任务,则可以使用字符串,但不建议这样做。

返回#

¥Returns

要注册为任务或嵌套在其他 series 和/或 parallel 组合中的组合操作。

¥A composed operation to be registered as a task or nested within other series and/or parallel compositions.

当执行组合操作时,所有任务将以最大并发度运行。如果一项任务中发生错误,其他任务可能会不确定地完成,也可能不会完成。

¥When the composed operation is executed, all tasks will be run at maximum concurrency. If an error occurs in one task, other tasks nondeterministically may or may not complete.

错误#

¥Errors

当没有任务被传递时,抛出错误并显示消息 "应使用串联或并行组合一项或多项任务"。

¥When no tasks are passed, throws an error with the message, "One or more tasks should be combined using series or parallel".

当传递无效任务或未注册的任务时,抛出错误并显示消息 "任务从未定义"。

¥When invalid tasks or unregistered tasks are passed, throws an error with the message, "Task never defined".

向前引用#

¥Forward references

前向引用是指当你使用尚未注册的字符串引用编写任务时。这是旧版本中的常见做法,但删除了此功能以实现更快的任务运行时间并促进命名函数的使用。

¥A forward reference is when you compose tasks, using string references, that haven't been registered yet. This was a common practice in older versions, but this feature was removed to achieve faster task runtime and promote the use of named functions.

在较新的版本中,如果你尝试使用前向引用,你将收到错误消息 "任务从未定义"。当你尝试使用 exports 进行任务注册并通过字符串编写任务时,你可能会遇到这种情况。在这种情况下,请使用命名函数而不是字符串引用。

¥In newer versions, you'll get an error, with the message "Task never defined", if you try to use forward references. You may experience this when trying to use exports for task registration and composing tasks by string. In this situation, use named functions instead of string references.

在迁移过程中,你可能需要 前向参考注册表。这将为每个任务引用添加额外的封闭,并显着减慢你的构建速度。不要长期依赖此修复。

¥During migration, you may need the forward reference registry. This will add an extra closure to every task reference and dramatically slow down your build. Don't rely on this fix for very long.

避免重复任务#

¥Avoid duplicating tasks

当运行组合操作时,每个任务将在每次提供时执行。

¥When a composed operation is run, each task will be executed every time it was supplied.

两个不同组合中引用的 clean 任务将运行两次并导致不期望的结果。相反,重构要在最终组合中指定的 clean 任务。

¥A clean task referenced in two different compositions would be run twice and lead to undesired results. Instead, refactor the clean task to be specified in the final composition.

如果你有这样的代码:

¥If you have code like this:

// This is INCORRECT
const { series, parallel } = require('gulp');
const clean = function(cb) {
// body omitted
cb();
};
const css = series(clean, function(cb) {
// body omitted
cb();
});
const javascript = series(clean, function(cb) {
// body omitted
cb();
});
exports.build = parallel(css, javascript);

迁移到这个:

¥Migrate to this:

const { series, parallel } = require('gulp');
function clean(cb) {
// body omitted
cb();
}
function css(cb) {
// body omitted
cb();
}
function javascript(cb) {
// body omitted
cb();
}
exports.build = series(clean, parallel(css, javascript));