JavaScript Deferred Promise Pattern - withResolvers

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

JavaScript Deferred Promise Pattern - withResolvers

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Motivation

The promise constructor within JavaScript assumes that we are going to do our asynchronous work within the constructor callback. But sometimes we want to give the promise to one party so that they can follow the async chain. And give the resolvers to another party so they can decide the fate of the promise.

Introduction

Default approach:

async function main() {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 1000);
});

await promise;
console.log('Done!');
}
main();

Moving the async task out of the promise constructor:

async function main() {
let { promise, resolve, reject } = Promise.withResolvers();

setTimeout(() => {
resolve();
}, 1000);

await promise;
console.log('Done!');
}
main();

Example Application

class onCallN {
count = 0;

constructor(n, nCallback) {
this.n = n;
this.nCallback = nCallback;
}

call() {
this.count++;
if (this.count % this.n === 0) {
this.nCallback();
}
}
}

async function main() {
const { promise, resolve } = Promise.withResolvers();
const everyThird = new onCallN(3, () => {
console.log('3 items');
resolve();
});

const interval = setInterval(() => everyThird.call(), 1000);

// Wait for three calls and then clear the interval
await promise;
clearInterval(interval);
}
main();
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

The Promise Constructor within JavaScript assumes that we are going to do our asynchronous work within the constructor callback, but sometimes you want to give the promise to one party so that they can follow the async chain and then give the resolvers to another party so that they can decide the fate of the promise. This is where the Wi Resolver function comes into play. So let's take a look. This example demonstrates the basic structure of a modern JavaScript promise. We have the Promise Constructor where we get the resolve and the reject methods, which can be used to determine the fate of the promise. And within the callback, we choose to carry out the work

00:34

that will eventually resolve or reject the promise. Now let's take a look at how we can potentially move the work out of the Promise Constructor. If we create variables outside of the Promise Constructor to store the resolve and the reject methods, we can set them up by using the values that are provided to the constructor callback. And this allows us to do the work outside of the Promise constructor and then use the resolve and the reject methods. Now this quote is definitely more robust in the quote that we saw previously, but fortunately it can be simplified thanks to the Promise with resolvers method that exists within modern JavaScript.

01:10

Now let's do a quick recap between the Promise constructive version versus the Promise With resolvers version, with the Promise Construction version, the work is being done within the Promise constructor callback versus with resolvers where we have the ability to determine the fate of the promise outside of the callback. The work can be carried out independent of the promise. Now that you have an understanding of what the pattern looks like and how it allows you to move the resolution logic outside of the promise constructor, let's take a look at the practical application. Consider a simple class that will only

01:42

call a provided callback. After end calls, we start off by tracking the count as zero. Within the constructor, we take the number of calls after which we will invoke the end callback, and then within the call function, we increment the count. And if it is a factor of the provided end, then we invoke the provided end callback. This is a pretty simple class with some pretty simple requirements, so let's try to use it within an async function. We set up an instance that will call a provided callback after every three invocations. Next, we set up an interval that will invoke the call method

02:16

after every second, and we have a very simple async requirement that after the third call, we should clear the interval and exit the program. I encourage you to pause the video at this point and think of a possible solution. Now, the solution to this problem is actually quite simple. We need an async resolution to happen over here to resolve a promise over here, and we can get these two pieces of the puzzle using the with resolvers method. We resolve the promise within the callback, and then we simply AWAI the promise to ensure

02:49

that the execution continues after the promise has been resolved. And once the promise is resolved, we can clear the interval. Now, this isn't the only solution To this problem, however, it does demonstrate how you can easily separate the promise and its resolvers if you are ever forced into a situation where they need to be used in different portions of the code base. One final thing worth mentioning is that with resolvers allows you to defer the resolution of the promise to some party outside of the promise constructor, which is the reason why it was traditionally called the deferred promise pattern.

03:22

The term deferred promise is not something that you should need to worry about anymore, but some old developers like me might still use that term, so it's good to know. As always, thank you for joining me and I'll see you in the next one.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36