I've seen several linters flag this behavior, but I'm wondering if that's not part of the reason you're using promises:
const promise = myFn() //do other stuff const result = myOtherFn(await promise)
Error: Promise should be awaited or captured
So is this a bad code? If so, why?
Yes, this is an unusual use of
await
and a bad practice that can cause your application to crash.Usually you will immediately
await
the promise:The problem with not
await
ing promises immediately is that you miss out when it rejects with an error while// do other stuff
is running. If the other thing is async you mayawait
too late, if the other thing itself throws the exception you neverawait
it, in both cases this will Resulting inpromise
unhandled rejection, which will crash your application. See also Awaiting Multiple Concurrent Wait Operations and await What is the difference between Promise.all() and multiple awaits? 一个>.