Skip to content

Commit 4169cb6

Browse files
janiceilenephated
authored andcommitted
Docs: Update series() documentation
1 parent d680487 commit 4169cb6

File tree

1 file changed

+94
-20
lines changed

1 file changed

+94
-20
lines changed

docs/api/series.md

+94-20
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,109 @@ hide_title: true
55
sidebar_label: series()
66
-->
77

8-
# `gulp.series(...tasks)`
8+
# series()
99

10-
Takes a number of task names or functions and returns a function of the composed
11-
tasks or functions.
10+
Combines task functions and/or composed operations into larger operations that will be executed one after another, in sequential order. The composed operations from `series()` and `parallel()` can be nested to any depth.
1211

13-
When using task names, the task should already be registered.
12+
## Usage
1413

15-
When the returned function is executed, the tasks or functions will be executed
16-
in series, each waiting for the prior to finish. If an error occurs,
17-
execution will stop.
14+
```js
15+
const { series } = require('gulp');
16+
17+
function javascript(cb) {
18+
// body omitted
19+
cb();
20+
}
21+
22+
function css(cb) {
23+
// body omitted
24+
cb();
25+
}
26+
27+
exports.build = series(javascript, css);
28+
```
29+
30+
## Signature
31+
32+
```js
33+
series(...tasks)
34+
```
35+
36+
### Parameters
37+
38+
| parameter | type | note |
39+
|:--------------:|:------:|-------|
40+
| tasks <br> **(required)** | function <br> string | Any number of task functions can be passed as individual arguments. Strings can be used if you've registered tasks previously, but this is not recommended. |
41+
42+
### Returns
43+
44+
A composed operation to be registered as a task or nested within other `series` and/or `parallel` compositions.
45+
46+
When the composed operation is executed, all tasks will be run sequentially. If an error occurs in one task, no subsequent tasks will be run.
47+
48+
### Errors
49+
50+
When no tasks are passed, throws an error with the message, "One or more tasks should be combined using series or parallel".
1851

52+
When invalid tasks or unregistered tasks are passed, throws an error with the message, "Task never defined".
53+
54+
## Forward references
55+
56+
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.
57+
58+
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 your task registration *and* composing tasks by string. In this situation, use named functions instead of string references.
59+
60+
During migration, you may need to use the [forward reference registry][undertaker-forward-reference-external]. 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**.
61+
62+
## Avoid duplicating tasks
63+
64+
When a composed operation is run, each task will be executed every time it was supplied.
65+
66+
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.
67+
68+
If you have code like this:
1969
```js
20-
gulp.task('one', function(done) {
21-
// do stuff
22-
done();
70+
// This is INCORRECT
71+
const { series, parallel } = require('gulp');
72+
73+
const clean = function(cb) {
74+
// body omitted
75+
cb();
76+
};
77+
78+
const css = series(clean, function(cb) {
79+
// body omitted
80+
cb();
2381
});
2482

25-
gulp.task('two', function(done) {
26-
// do stuff
27-
done();
83+
const javascript = series(clean, function(cb) {
84+
// body omitted
85+
cb();
2886
});
2987

30-
gulp.task('default', gulp.series('one', 'two', function(done) {
31-
// do more stuff
32-
done();
33-
}));
88+
exports.build = parallel(css, javascript);
3489
```
3590

36-
## tasks
37-
Type: `Array`, `String` or `Function`
91+
Migrate to this:
92+
```js
93+
const { series, parallel } = require('gulp');
94+
95+
function clean(cb) {
96+
// body omitted
97+
cb();
98+
}
99+
100+
function css(cb) {
101+
// body omitted
102+
cb();
103+
}
104+
105+
function javascript(cb) {
106+
// body omitted
107+
cb();
108+
}
109+
110+
exports.build = series(clean, parallel(css, javascript));
111+
```
38112

39-
A task name, a function or an array of either.
113+
[undertaker-forward-reference-external]: https://github.com/gulpjs/undertaker-forward-reference

0 commit comments

Comments
 (0)