Why does using await
need its outer function to be declared async
?
For example, why does this mongoose statement need the function it's in to return a promise?
async function middleware(hostname, done) {
try {
let team = await Teams.findOne({ hostnames: hostname.toLowerCase() }).exec();
done(null, team);
} catch (err) { done(err); }
}
I see the runtime/transpiler resolving the Teams promise to it's value and async signaling it "throws" rejected promises.
But try/catch "catches" those rejected promises, so why are async and await so tightly coupled?
async
keyword? – Bergi