Skip to content

Commit 3cf2f5d

Browse files
committed
docs: update promise methods docs
1 parent 9709a97 commit 3cf2f5d

9 files changed

Lines changed: 642 additions & 93 deletions

File tree

docs/standard-built-in-objects/control-abstraction-objects/promise/constructor/all-settled.md

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,66 @@ order: 8
1212

1313
# Promise.allSettled
1414

15+
⭐️ `ES2020(ES11)新特性`
16+
1517
`Promise.allSettled` 方法返回一个在所有给定的 Promise 都已经 `fullfilled``rejected` 后的 Promise,并带有一个对象数组,每个对象表示对应的 Promise 结果。
1618

1719
当您有多个彼此不依赖的异步任务成功完成时,或者您总是想知道每个 Promise 的结果时,通常使用它。
1820

1921
## 语法
2022

23+
语法:
24+
2125
```js
22-
Promise.allSettled(iterable)
26+
Promise.allSettled(iterable);
27+
```
28+
29+
类型声明:
2330

24-
Promise.allSettled([promise1, promise2, ..., promiseN])
31+
```ts
32+
interface PromiseFulfilledResult<T> {
33+
status: 'fulfilled';
34+
value: T;
35+
}
36+
37+
interface PromiseRejectedResult {
38+
status: 'rejected';
39+
reason: any;
40+
}
41+
42+
type PromiseSettledResult<T> = PromiseFulfilledResult<T> | PromiseRejectedResult;
43+
44+
interface PromiseConstructor {
45+
allSettled<T extends readonly unknown[] | readonly [unknown]>(
46+
values: T
47+
): Promise<
48+
{ -readonly [P in keyof T]: PromiseSettledResult<T[P] extends PromiseLike<infer U> ? U : T[P]> }
49+
>;
50+
51+
allSettled<T>(
52+
values: Iterable<T>
53+
): Promise<PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[]>;
54+
}
2555
```
2656

27-
### 参数
57+
参数说明:
58+
59+
| 参数 | 说明 | 类型 |
60+
| :------- | :----- | :--- |
61+
| iterable | 见下方 | any |
62+
63+
根据传入参数的不同,会有不同的响应效果:
2864

29-
参数 `iterable` 必须具备 [Iterator](../../iterator-objects/iterator) 接口,且每个成员都是 Promise 实例。
65+
- 空的具备 Iterator 接口的对象,返回状态为 Fulfilled 的 Promise
66+
- 不包含任何 Promise,返回异步完成的 Promise
67+
- 其他情况,返回状态为 Pending 的 Promise
3068

31-
如果 `iterable` 内每个成员都不是 Promise 实例,会先调用 [Promise.resolve](resolve) 将每个成员转化为 Promise 实例,再进一步处理。
69+
<br />
3270

33-
| 参数 | 返回值 |
34-
| :--------------------------- | :---------------------------- |
35-
| 空的具备 Iterator 接口的对象 | 状态为 `fulfilled` 的 Promise |
36-
| 不包含任何 Promise | 异步完成的 Promise |
37-
| 其他情况 | 状态为 `pending` 的 Promise |
71+
- 参数 `iterable` 必须具备 [Iterator](../../iterator-objects/iterator) 接口,且每个成员都是 Promise 实例
72+
- 如果 `iterable` 内每个成员都不是 Promise 实例,会先调用 [Promise.resolve](resolve) 将每个成员转化为 Promise 实例,再进一步处理
3873

39-
### 描述
74+
## 方法描述
4075

4176
对于 `Promise.allSettled` 执行集合中的每个 Promise 都已经完成后,无论时成功(`fulfiiled`)或是拒绝(`rejected`),未决议的 Promise 将被异步完成。那时,所返回的 Promise 的处理器将传入一个数组作为输入,该数组包含原始 Promise 集合中每个 Promise 的结果。
4277

@@ -47,7 +82,7 @@ Promise.allSettled([promise1, promise2, ..., promiseN])
4782

4883
`value``reason` 分别反映了每个 Promise 决议(或拒绝)的值。
4984

50-
## 示例
85+
## 代码示例
5186

5287
应用场景:
5388

docs/standard-built-in-objects/control-abstraction-objects/promise/constructor/all.md

Lines changed: 105 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,121 @@ order: 4
1212

1313
# Promise.all
1414

15-
`Promise.all` 接收一个以 Promise 实例为成员的可迭代对象作为参数,当所有输入的 Promise 成员全部变为 Fulfilled 状态时才会继续执行后续的 `Promise.prototype.then`,如果某个成员变为 Rejected 的时候,函数后续的 `Promise.prototype.catch` 会被执行。
15+
`Promise.all` 接收一个以 Promise 实例为成员的可迭代对象作为参数,当所有输入的 Promise 成员全部变为 Fulfilled 状态时才会继续执行后续的 [Promise.prototype.then()](./then),如果某个成员变为 Rejected 的时候,函数后续的 [Promise.prototype.catch()](./catch) 会被执行。
16+
17+
## 语法
1618

1719
语法:
1820

1921
```js
20-
Promise.all(iterable)
21-
22-
Promise.all([promise1, promise2, ..., promiseN])
22+
Promise.all(values);
2323
```
2424

25-
参数:
26-
27-
- `iterable`:必须具备 [Iterator](../../iterator-objects/iterator) 接口,且每个成员都是 Promise 实例。
28-
29-
如果 `iterable` 内每个成员都不是 Promise 实例,会先调用 [Promise.resolve](resolve) 将每个成员转化为 Promise 实例,再进一步处理。
30-
31-
| 参数 | 返回值 |
32-
| :--------------------------- | :----------------------- |
33-
| 空的具备 Iterator 接口的对象 | Fulfilled 状态的 Promise |
34-
| 不包含任何 Promise | 异步完成的 Promise |
35-
| 其他情况 | Pending 状态的 Promise |
36-
3725
类型声明:
3826

3927
```ts
4028
interface PromiseConstructor {
41-
all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>;
29+
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
30+
values: readonly [
31+
T1 | PromiseLike<T1>,
32+
T2 | PromiseLike<T2>,
33+
T3 | PromiseLike<T3>,
34+
T4 | PromiseLike<T4>,
35+
T5 | PromiseLike<T5>,
36+
T6 | PromiseLike<T6>,
37+
T7 | PromiseLike<T7>,
38+
T8 | PromiseLike<T8>,
39+
T9 | PromiseLike<T9>,
40+
T10 | PromiseLike<T10>
41+
]
42+
);
43+
44+
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
45+
values: readonly [
46+
T1 | PromiseLike<T1>,
47+
T2 | PromiseLike<T2>,
48+
T3 | PromiseLike<T3>,
49+
T4 | PromiseLike<T4>,
50+
T5 | PromiseLike<T5>,
51+
T6 | PromiseLike<T6>,
52+
T7 | PromiseLike<T7>,
53+
T8 | PromiseLike<T8>,
54+
T9 | PromiseLike<T9>
55+
]
56+
);
57+
58+
all<T1, T2, T3, T4, T5, T6, T7, T8>(
59+
values: readonly [
60+
T1 | PromiseLike<T1>,
61+
T2 | PromiseLike<T2>,
62+
T3 | PromiseLike<T3>,
63+
T4 | PromiseLike<T4>,
64+
T5 | PromiseLike<T5>,
65+
T6 | PromiseLike<T6>,
66+
T7 | PromiseLike<T7>,
67+
T8 | PromiseLike<T8>
68+
]
69+
);
70+
71+
all<T1, T2, T3, T4, T5, T6, T7>(
72+
values: readonly [
73+
T1 | PromiseLike<T1>,
74+
T2 | PromiseLike<T2>,
75+
T3 | PromiseLike<T3>,
76+
T4 | PromiseLike<T4>,
77+
T5 | PromiseLike<T5>,
78+
T6 | PromiseLike<T6>,
79+
T7 | PromiseLike<T7>
80+
]
81+
);
82+
all<T1, T2, T3, T4, T5, T6>(
83+
values: readonly [
84+
T1 | PromiseLike<T1>,
85+
T2 | PromiseLike<T2>,
86+
T3 | PromiseLike<T3>,
87+
T4 | PromiseLike<T4>,
88+
T5 | PromiseLike<T5>,
89+
T6 | PromiseLike<T6>
90+
]
91+
);
92+
all<T1, T2, T3, T4, T5>(
93+
values: readonly [
94+
T1 | PromiseLike<T1>,
95+
T2 | PromiseLike<T2>,
96+
T3 | PromiseLike<T3>,
97+
T4 | PromiseLike<T4>,
98+
T5 | PromiseLike<T5>
99+
]
100+
);
101+
all<T1, T2, T3, T4>(
102+
values: readonly [
103+
T1 | PromiseLike<T1>,
104+
T2 | PromiseLike<T2>,
105+
T3 | PromiseLike<T3>,
106+
T4 | PromiseLike<T4>
107+
]
108+
);
109+
all<T1, T2, T3>(
110+
values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]
111+
);
112+
all<T1, T2>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]);
113+
all<T>(values: readonly [T | PromiseLike<T>]);
42114
}
43115
```
44116

45-
### 描述
117+
参数说明:
118+
119+
| 参数 | 参数说明 | 类型 |
120+
| :----- | :------- | :--- |
121+
| values | 见下方 | any |
122+
123+
- 空的具备 Iterator 接口的对象,将返回 Fulfilled 状态的 Promise |
124+
- 不包含任何 Promise,将返回异步完成的 Promise |
125+
- 其他情况,将返回 Pending 状态的 Promise |
126+
- `iterable`:必须具备 [Iterator](../../iterator-objects/iterator) 接口,且每个成员都是 Promise 实例。
127+
- 如果 `iterable` 内每个成员都不是 Promise 实例,会先调用 [Promise.resolve](resolve) 将每个成员转化为 Promise 实例,再进一步处理。
128+
129+
## 方法说明
46130

47131
`Promise.all` 执行后返回一个新创建的 Promise 实例,该实例状态由 `Promise.all` 参数成员决定,可以分为两种情况:
48132

@@ -71,7 +155,7 @@ Promise.all([p1, p2])
71155
.catch((err) => console.log(err));
72156
```
73157

74-
## 示例
158+
## 代码示例
75159

76160
### 基本用法
77161

@@ -92,7 +176,7 @@ Promise.all([p1, p2, p3]).then((v) => console.log(v));
92176

93177
`Promise.all` 在任意一个传入的 Promise 否决时返回新的 Rejected 状态的 Promise 实例。
94178

95-
例如,如果你传入的 Promise 中,有四个 Promise 实例在一定的时间之后调用成功函数,有一个立即调用失败函数,那么 `Promise.all` 将立即变为 _Rejected_ 状态。
179+
例如,如果你传入的 Promise 中,有四个 Promise 实例在一定的时间之后调用成功函数,有一个立即调用失败函数,那么 `Promise.all` 将立即变为 Rejected 状态。
96180

97181
```js
98182
var p1 = new Promise((resolve, reject) => {
@@ -152,4 +236,5 @@ Promise.all(
152236

153237
## 参考资料
154238

239+
- [TypeScript - lib.es2015.promise.d.ts](https://github.com/microsoft/TypeScript/blob/main/lib/lib.es2015.promise.d.ts)
155240
- [📝 Promise.all 处理 Rejection 的技巧](https://zhuanlan.zhihu.com/p/26920718)

0 commit comments

Comments
 (0)