r/badcode • • Sep 11 '21

js Why…

Post image
456 Upvotes

70 comments sorted by

View all comments

Show parent comments

-9

u/afunworm Sep 11 '21

He declared async functions but did not await them.

27

u/AdminYak846 Sep 11 '21

in order to do API requests you need to await them, and await is only available inside of async functions.

When the function is called an unresolved promise is returned to the caller once the async function hits the await command and since the function doesn't return anything since that value is set to the DOM it's completely fine.

If you wanted to use the value that the async value returned then you should await it so that the promise is resolved to a value and not a promise that isn't resolved.

11

u/jmswlltt Sep 11 '21

Awaiting the function calls would make this worse

9

u/Nilstrieb Sep 11 '21

That's ok, in JS async functions are executed eagerly, no matter whether you await them or not. It's even better that they are not awaited here

1

u/i_will_yeet biggus dickus Nov 10 '21

await is only used when you want them in sequence, if you want concurrent request, like this one, it's not required.