Some work finishes later, such as reading data, waiting on a server, or loading a file. Promises and async functions help you manage that delayed work clearly.
A promise represents a result that is not ready yet. It may resolve later with a value or reject with an error.
async function getMessage(): Promise<string> {
return "Loaded data";
}
getMessage().then((message) => {
console.log(message);
});
async function run(): Promise<void> {
const message = await getMessage();
console.log(message);
}
run();
async on functions that use await
await pauses inside that function until the promise
resolves
try and catch when
failure is possible