ES7 - How to stop(cut) async/await chaining
Asked Answered
S

1

6

If I use async/await function in a nested function call, I think the callers of that async/await functions should have async/await prefix.

For example, in this situation:

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

...

function y() {
  z();
}

If z was async function, those functions should be:

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

...

async function y() {
  await z();
}

When/How is it appropriate to stop the chaining of async/await?

Sink answered 6/3, 2018 at 2:34 Comment(1)
@guijob await have to be in async function, haven't it?Sink
A
3

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(...)

Or async IIFE:

(async () => {
  try {
    await a();
  } catch (e) { ... }
})();
Aplanatic answered 6/3, 2018 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.