By immediately resolving the promise
In this method, we will resolve the promise immediately after declaring the instance of the new Promise() object using the Promise.resolve() method and create an empty promise.
Syntax:
new Promise((resolve)=>{resolve();});
Example: The below code example creates an empty promise by immediately resolving it.
function createEmptyPromise(): Promise<void> {
return new Promise((resolve) => {
resolve();
});
}
createEmptyPromise().then(() => {
console.log(`Empty Promise is created
by immediately resolving
it after creating instance
of Promise object.`);
});
Output:
Empty Promise is created by immediately resolving it after creating instance of Promise object.
How to Return an Empty Promise in TypeScript ?
In TypeScript, you can return an empty promise to create an empty asynchronous task.
There are mainly two ways that can be used to create empty promises in TypeScript as listed below.
Table of Content
- By using the Promise.resolve() method
- By immediately resolving the promise
- Using async/await to Return an Empty Promise
Contact Us