ES7 - 如何停止(切断)异步/等待链

yak*_*you 5 javascript async-await ecmascript-2017

如果我在嵌套函数调用中使用 async/await 函数,我认为该 async/await 函数的调用者应该具有 async/await 前缀。

例如,在这种情况下:

function a() {
  b();
}
function b() {
  c();
}
function c() {
  d();
}

...

function y() {
  z();
}
Run Code Online (Sandbox Code Playgroud)

如果z是异步函数,这些函数应该是:

async function a() {
  await b();
}
async function b() {
  await c();
}
async function c() {
  await d();
}

...

async function y() {
  await z();
}
Run Code Online (Sandbox Code Playgroud)

何时/如何适当地停止 async/await 的链接?

Est*_*ask 2

async function is just syntactic sugar for promises. It's a function that returns a promise and should be treated like one.

At some point there should have either:

a().catch(...)
Run Code Online (Sandbox Code Playgroud)

Or async IIFE:

(async () => {
  try {
    await a();
  } catch (e) { ... }
})();
Run Code Online (Sandbox Code Playgroud)