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.

Javascript
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

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