You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/components_guide_web/templates/web_standards/promise.html.md
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Promises act as values
4
4
5
-
You can think of a Promise as an eventual value. It either succeeds with a particular success value, or it possibly fails with an error value.
5
+
You can think of a Promise as an eventual value. This value could be a success value or a failure value. It either succeeds with a particular successful result, or it fails with an error. Importantly, it can’t be both.
@@ -65,20 +65,20 @@ Or it *rejects* with an error value:
65
65
66
66
```javascript
67
67
// Fail with the error *out of stock*
68
-
Promise.reject(newError("out of stock"));
68
+
Promise.reject(Error("out of stock"));
69
69
70
70
// Longer version of above
71
71
newPromise((resolve, reject) => {
72
-
reject(newError("out of stock"));
72
+
reject(Error("out of stock"));
73
73
});
74
74
75
75
// Alternate version of above
76
76
newPromise((resolve, reject) => {
77
-
thrownewError("out of stock");
77
+
throwError("out of stock");
78
78
});
79
79
```
80
80
81
-
Once a Promise has receive its value, you can’t changed that value. If it was told it failed, it can’t be changed to succeed instead. And if it succeeded, it can’t later fail.
81
+
Once a Promise has receive its value, you can’t changed that value. If it was told it failed, it can’t be later told to succeed instead. And if it succeeded, it can’t later fail.
82
82
83
83
```javascript
84
84
newPromise((resolve, reject) => {
@@ -87,7 +87,7 @@ new Promise((resolve, reject) => {
87
87
// it can’t then be resolved again
88
88
resolve(42); // Invalid!
89
89
// or be rejected later
90
-
reject(newError("out of stock")); // Invalid!
90
+
reject(Error("out of stock")); // Invalid!
91
91
});
92
92
```
93
93
@@ -169,18 +169,18 @@ The callback to `.then()` can fail, either by throwing an error, or returning a
169
169
```javascript
170
170
Promise.resolve(42)
171
171
.then(value=> {
172
-
thrownewError("out of stock");
172
+
throwError("out of stock");
173
173
});
174
174
175
175
// Longer version of above
176
176
Promise.resolve(42)
177
177
.then(value=> {
178
-
returnPromise.reject(newError("out of stock"));
178
+
returnPromise.reject(Error("out of stock"));
179
179
});
180
180
181
181
// Alternative version of above
182
182
// Note the Promises can be created up-front.
183
-
constoutOfStockError=Promise.reject(newError("out of stock"));
183
+
constoutOfStockError=Promise.reject(Error("out of stock"));
184
184
Promise.resolve(42)
185
185
.then(value=> {
186
186
return outOfStockError;
@@ -230,7 +230,7 @@ If a Promise fails, any derived Promises will also fail.
230
230
```javascript
231
231
Promise.resolve(42)
232
232
.then(value=> {
233
-
thrownewError("out of stock");
233
+
throwError("out of stock");
234
234
})
235
235
.then(value=> {
236
236
// This will never get called as the previous promise was rejected
@@ -283,7 +283,7 @@ A Promise chain can be recovered by calling `.catch()` and returning another val
283
283
```javascript
284
284
exportconsta=Promise.resolve(42)
285
285
.then(value=> {
286
-
thrownewError("out of stock");
286
+
throwError("out of stock");
287
287
})
288
288
.catch(error=> {
289
289
return3;
@@ -292,7 +292,7 @@ export const a = Promise.resolve(42)
292
292
// Same as above
293
293
exportconstb=Promise.resolve(42)
294
294
.then(value=> {
295
-
thrownewError("out of stock");
295
+
throwError("out of stock");
296
296
})
297
297
.catch(error=> {
298
298
returnPromise.resolve(3);
@@ -302,15 +302,15 @@ export const b = Promise.resolve(42)
0 commit comments