Open In App

How to handle asynchronous operation in TypeScript?

Last Updated : 23 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The asynchronous operations in TypeScript can be performed in the same way as we usually do in simple JavaScript. We can use the Promises, Callbacks, and async/await statements to handle the asynchronous operation in TypeScript. The below syntax will show how you can use different approaches to handle the asynchronous operations in TypeScript.

Syntax:

// Using Promises
function function_name(): Promise<string>{
return new Promise(()=>{
// Resolve promise here
});
}

// Using Callbacks
function function_name((data: string, error?: Error) => {
// Resolve callback here
});

// Using async/await
function func1(delay: number): Promise<void>{
return new Promise(()=>{
// Resolve Promise here
})
}

async function function_name(): Promise<string>{
const pro = await func1(2000);
return pro;
}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads