By using the Promise.resolve() method
In this approach, we will directly use the Promise.resolve() method instead of creating an instance of a new Promise() object to create an empty object or a dummy asynchronous task.
Syntax:
Promise.resolve();
Example: The below code example will explain the use of the promise.resolve() method creates an empty promise.
function createEmptyPromise(): Promise<void> {
return Promise.resolve();
}
createEmptyPromise().then(() => {
console.log(`Empty Promise created
using Promise.resolve() method.`);
});
Output:
Empty Promise created using Promise.resolve() method.
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