Skip to content

Commit 059d90d

Browse files
committed
Add Promise resolvers in ES2024
1 parent 6b1192d commit 059d90d

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

README.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
122122
|3 | [Well formed unicode strings](#well-formed-unicode-strings) |
123123
|4 | [Atomic waitSync](#atomic-waitsync) |
124124
|5 | [RegEx v Flag and string properties](#regEx-v-flag-and-string-properties) |
125-
|6 | [Pipeline Operator](#pipeline-operator) |
126-
|7 | [Records and Tuples](#records-and-tuples) |
127-
|8 | [Decorators](#decorators) |
125+
|6 | [Promise withResolvers](#promise-withResolvers) |
126+
|7 | [Pipeline Operator](#pipeline-operator) |
127+
|8 | [Records and Tuples](#records-and-tuples) |
128+
|9 | [Decorators](#decorators) |
128129

129130
## ES2015 Or ES6
130131

@@ -2706,5 +2707,38 @@ Most of these features already supported by some browsers and try out with babel
27062707
**Note:** There will be a TypeError if typedArray is not an **Int32Array** or **BigInt64Array** that views a SharedArrayBuffer.
27072708
27082709
5. ### RegEx v Flag and string properties
2710+
6. ### Promise withResolvers
2711+
When you are creating a new Promise object, usually you pass `resolve` and `reject` functions to the executor of promise constructor as shown below:
2712+
2713+
```javascript
2714+
const promise = new Promise((resolve, reject) =>{
2715+
setTimeout(() => { Math.random() > 0.5 ? resolve("Success") : reject("Error")}, 1000);
2716+
});
2717+
promise.then(result => console.log(result)).catch(error => console.error(error));
2718+
```
2719+
2720+
In this constructor pattern, it is possile call the `resolve` and `reject` functions inside the promise constructor only. But if you want these functions outside of the promise constructor, you often have to write the following boilerplate code.
2721+
2722+
```javascript
2723+
let resolve, reject;
2724+
const promise = new Promise((res, rej) => {
2725+
resolve = res;
2726+
reject = rej;
2727+
});
2728+
2729+
setTimeout(() => { Math.random() > 0.5 ? resolve("Success") : reject("Error")}, 1000);
2730+
promise.then(result => console.log(result)).catch(error => console.error(error));
2731+
```
2732+
2733+
The above code is simplified with `Promise.withResolvers()` in ES2024, it's a static method factory that returns an object containing a new Promise along with two functions one for resolve and other one for reject. These two functions corresponding to the two parameters passed to the executor of the Promise() constructor shown in the initial code snippets.
2734+
2735+
The concise version of earlier code snippet looks like below,
2736+
2737+
```javascript
2738+
const { promise, resolve, reject} = Promise.withResolvers();
2739+
2740+
setTimeout(() => { Math.random() > 0.5 ? resolve("Success") : reject("Error")},1000);
2741+
promise.then(result => console.log(result)).catch(error => console.error(error));
2742+
```
27092743
27102744
**[⬆ Back to Top](#table-of-contents)**

es2024/6.promise-withResolvers.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { promise, resolve, reject} = Promise.withResolvers();
2+
3+
setTimeout(() => { Math.random() > 0.5 ? resolve("Success") : reject("Error")},1000);
4+
promise.then(result => console.log(result)).catch(error => console.error(error));

0 commit comments

Comments
 (0)