Bálint Orosz

Create Promise out of setTimeout

I always forget how to make a awaitable Promise out of setTimeout. It it pretty useful for testing loading states to make e.g. 10 sec timeout to see how it will look like. (network throtting works fine as well, but sometimes it isn't enough).

async function wait(ms: number) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

And you can call it like this:

async function fetchData(): Promise<Data> {
    await wait(10_000); // 10s

    const res = await fetch("");
    return res.json();
}