Skip to content

Commit ecb0cac

Browse files
committed
Improve promise article
1 parent bf634dc commit ecb0cac

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/components_guide_web/templates/web_standards/promise.html.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Promises act as values
44

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.
66

77
<figure>
88
<div class="my-4 flex text-center text-3xl text-black bg-white rounded shadow-lg">
@@ -65,20 +65,20 @@ Or it *rejects* with an error value:
6565

6666
```javascript
6767
// Fail with the error *out of stock*
68-
Promise.reject(new Error("out of stock"));
68+
Promise.reject(Error("out of stock"));
6969

7070
// Longer version of above
7171
new Promise((resolve, reject) => {
72-
reject(new Error("out of stock"));
72+
reject(Error("out of stock"));
7373
});
7474

7575
// Alternate version of above
7676
new Promise((resolve, reject) => {
77-
throw new Error("out of stock");
77+
throw Error("out of stock");
7878
});
7979
```
8080

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.
8282

8383
```javascript
8484
new Promise((resolve, reject) => {
@@ -87,7 +87,7 @@ new Promise((resolve, reject) => {
8787
// it can’t then be resolved again
8888
resolve(42); // Invalid!
8989
// or be rejected later
90-
reject(new Error("out of stock")); // Invalid!
90+
reject(Error("out of stock")); // Invalid!
9191
});
9292
```
9393

@@ -169,18 +169,18 @@ The callback to `.then()` can fail, either by throwing an error, or returning a
169169
```javascript
170170
Promise.resolve(42)
171171
.then(value => {
172-
throw new Error("out of stock");
172+
throw Error("out of stock");
173173
});
174174

175175
// Longer version of above
176176
Promise.resolve(42)
177177
.then(value => {
178-
return Promise.reject(new Error("out of stock"));
178+
return Promise.reject(Error("out of stock"));
179179
});
180180

181181
// Alternative version of above
182182
// Note the Promises can be created up-front.
183-
const outOfStockError = Promise.reject(new Error("out of stock"));
183+
const outOfStockError = Promise.reject(Error("out of stock"));
184184
Promise.resolve(42)
185185
.then(value => {
186186
return outOfStockError;
@@ -230,7 +230,7 @@ If a Promise fails, any derived Promises will also fail.
230230
```javascript
231231
Promise.resolve(42)
232232
.then(value => {
233-
throw new Error("out of stock");
233+
throw Error("out of stock");
234234
})
235235
.then(value => {
236236
// 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
283283
```javascript
284284
export const a = Promise.resolve(42)
285285
.then(value => {
286-
throw new Error("out of stock");
286+
throw Error("out of stock");
287287
})
288288
.catch(error => {
289289
return 3;
@@ -292,7 +292,7 @@ export const a = Promise.resolve(42)
292292
// Same as above
293293
export const b = Promise.resolve(42)
294294
.then(value => {
295-
throw new Error("out of stock");
295+
throw Error("out of stock");
296296
})
297297
.catch(error => {
298298
return Promise.resolve(3);
@@ -302,15 +302,15 @@ export const b = Promise.resolve(42)
302302
const fallbackPromise = Promise.resolve(3);
303303
export const c = Promise.resolve(42)
304304
.then(value => {
305-
throw new Error("out of stock");
305+
throw Error("out of stock");
306306
})
307307
.catch(error => {
308308
return fallbackPromise;
309309
});
310310

311311
// Same as above
312312
const promiseThatWillFail = Promise.resolve(42).then(value => {
313-
throw new Error("out of stock");
313+
throw Error("out of stock");
314314
});
315315
export const d = promiseThatWillFail.catch(error => {
316316
return fallbackPromise;
@@ -508,6 +508,7 @@ function main() {
508508
promise.then(data => {
509509
// Do nothing
510510
});
511+
511512
promise.then(data => {
512513
// Do nothing
513514
});

0 commit comments

Comments
 (0)