Async and forgotten await

Daijiro Wachi
2 min readMay 2, 2019

--

I was casting a sideways glance at the recent popularity of TypeScript aside and thinking that I would become the last person who continues writing JavaScript until today.

I was writing a simple function with async/await like the below.

function delay(msec, count) {
return new Promise<number>(resolve => {
setTimeout(() => {
resolve(count);
}, msec);
});
}
async function dramatic() {
for (let i = 0; i < 5; i++) {
const count = delay(500, i);
console.log(count);
}
}
dramatic();

You would realise that this code will make an error in the dramatic function. delay(500, i); should be awaited, but I forgot to put that. I spent 3 hours to figure out what was the problem and I hated myself after I found the line. Learning JavaScript is something like that.

Apparently, my friend Martin also found the same conundrum of async/await and he came up with TypeScript.

I used to use TypeScript in 2013 but did not like it since it ate my time at debugging. That was not about TypeScript itself, but it was about compiling + poor source map support at the time.

Compile into JavaScript is already a typical process in front-end development nowadays and I do not hear any problems around source map(I need node-source-map-support sometimes for some cases).

Then I just wrote the same codes in TypeScript and here is the result.

Playground

I’d start using TypeScript again.

--

--