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.

Javascript
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

Similar Reads

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

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

Using async/await to Return an Empty Promise

You can also leverage async and await syntax to return an empty promise in TypeScript. This method offers a more modern and concise way to handle asynchronous operations....

Contact Us