Région de recherche :

Date :

https://stackoverflow.com › questions › 49906865

Typescript - Wait for promise resolve before function return

For APIs that don't support promises, you can use new Promise to create a promise yourself, call resolve with the result of the async call, then you can use async/await syntax when calling it.

https://stackoverflow.com › questions › 27759593

How do I wait for a promise to finish before returning the variable of ...

The async function will still return a promise object if called without an await (or in non asynchronous code). Check the result of console.log (waitForPromise ()) if you are uncertain.

https://blog.logrocket.com › async-await-typescript

A guide to async/await in TypeScript - LogRocket Blog

An async function always returns a promise. Even if you omit the Promise keyword, the compiler will wrap your function in an immediately resolved promise. Here’s an example: //Snippet 1 const myAsynFunction = async (url: string): Promise<T> => { const { data } = await fetch(url) return data.

A guide to async/await in TypeScript - LogRocket Blog

https://blog.bitsrc.io › keep-your-promises-in-typescript-using-async-await-7bdc57041308

Keep Your Promises in TypeScript using async/await

But we can make things much simpler using async await. All we need to do to use async await is to create a Promise based delay function. This function takes a number of milliseconds and returns a Promise that gets resolved using setTimeout after the given number of milliseconds.

Keep Your Promises in TypeScript using async/await

https://upmostly.com › typescript › the-future-of-asynchronous-programming-promises-in...

The Future of Asynchronous Programming: Promises in TypeScript

Thankfully, TypeScript provides functionality to run code after your promise is resolved or rejected and to get the inner value your promise returns. These are the async/await syntax, and then () syntax.

The Future of Asynchronous Programming: Promises in TypeScript

https://dev.to › alakkadshaw › asyncawait-in-typescript-a-step-by-step-guide-4m1e

Async/Await in TypeScript: A step by step guide - DEV Community

Await Keyword: Within the async function you can use the await keyword to pause the execution of the function untill the Promise is resolved. The await keyword basically waits for the promise to be resolved and then returns the result of the promoise, which could either be success or failure.

https://dev.to › ... › understanding-asyncawait-and-promises-in-javascript-and-typescript-d0n

Understanding Async/Await and Promises in JavaScript and TypeScript

With await: Use it to process the resolved value of a Promise or for inline error handling, providing a clearer and more manageable flow. Understanding when and how to use await with Promises in JavaScript and TypeScript is key to writing code that's not just functional but also clean and readable.

Understanding Async/Await and Promises in JavaScript and TypeScript

https://bobbyhadz.com › blog › javascript-wait-promise-resolve-before-returning

Wait for a Promise to Resolve before Returning in JS - bobbyhadz

You can use the async/await syntax or call the .then() method on a promise to wait for it to resolve. Inside functions marked with the async keyword, you can use await to wait for the promises to resolve before continuing to the next line of the function.

Wait for a Promise to Resolve before Returning in JS - bobbyhadz

https://dev.to › cliff123tech › mastering-async-programming-in-typescript-promises-async...

Mastering Async Programming in TypeScript: Promises, Async/Await, and ...

const functionName = async (): Promise<ReturnType> => { try { const result = await promise; // code to execute after promise resolves return result; } catch (error) { // code to execute if promise rejects throw error; } };

Mastering Async Programming in TypeScript: Promises, Async/Await, and ...

https://upmostly.com › typescript › how-to-delay-a-function-in-typescript

How To Delay a Function in TypeScript - Upmostly

Promise. By wrapping a promise around setTimeout, we can await it, and thus delay all of the code until that timeout finishes. Here’s the promise-ified setTimeout (): function delay(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } And here’s how to use it: