最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS asyncawait - why does await need async? - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Sep 5, 2019 at 19:47 temporary_user_name 37.1k48 gold badges160 silver badges243 bronze badges asked May 25, 2017 at 15:16 Michael ColeMichael Cole 16.2k7 gold badges89 silver badges98 bronze badges 5
  • I don't get what you mean by "But try/catch "catches" those rejected promises". How is that relevant for the async keyword? – Bergi Commented May 25, 2017 at 20:15
  • Possible duplicate of Why is it necessary to have the async keyword? – Bergi Commented May 25, 2017 at 20:19
  • "why does this mongoose statement need the function it's in to return a promise?" - how else would it be able to wait for the asynchronous result of the mongoose promise? – Bergi Commented May 25, 2017 at 20:20
  • Here's a reason for the language design: stackoverflow.com/a/41744179/1483977 – Michael Cole Commented May 26, 2017 at 21:06
  • It should have been enforced “Async all the way” like in C# but it was half baked into JS :( ... the way it was implemented in JS ends up just some text that coders learn they have to paste or it doesn't work – – kofifus Commented Jun 2, 2023 at 5:34
Add a comment  | 

3 Answers 3

Reset to default 10

I'm not privy to the JavaScript language design discussions, but I assume it's for the same reasons that the C# language requires async (also see my blog).

Namely:

  1. Backwards compatibility. If await was suddenly a new keyword everywhere, then any existing code using await as a variable name would break. Since await is a contextual keyword (activated by async), only code that intends to use await as a keyword will have await be a keyword.
  2. Easier to parse. async makes asynchronous code easier to parse for transpilers, browsers, tools, and humans.

Copied from https://stackoverflow.com/a/41744179/1483977 by @phaux:

These answers all give valid arguments for why the async keyword is a good thing, but none of them actually mentions the real reason why it had to be added to the spec.

The reason is that this was a valid JS pre-ES7

function await(x) {
  return 'awaiting ' + x
}

function foo() {
  return(await(42))
}

According to your logic, would foo() return Promise{42} or "awaiting 42"? (returning a Promise would break backward compatibility)

So the answer is: await is a regular identifier and it's only treated as a keyword inside async functions, so they have to be marked in some way.

Fun fact: the original spec proposed more lightweight function^ foo() {} for async syntax.

Because using await inside middleware function means the middleware function can't return a result immediately (it must wait until await is settled) and middleware function callers must wait until the promise (returned from middleware function) is settled.

发布评论

评论列表(0)

  1. 暂无评论