RUN

Async and await in TypeScript

Wait for promises with typed results. Press Run to try it, then change the code and run it again.

main.ts

const wait = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));

async function fetchScore(player: string): Promise<number> {
  await wait(300);
  return player.length * 10;
}

const scores = await Promise.all(['Asha', 'Rohan', 'Mei'].map(fetchScore));
console.log('Scores:', scores);
Output

Press Run to see the output.

More TypeScript examples

Open the full TypeScript online compiler →