Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript await on multiple chained async functions

Say I have the following:

const a = new A();
await a.getB().action();

A.prototype.getB() is async as-well as B.prototype.action(). If I try to await on the chaining of the functions I get an error: TypeError: a.getB(...).action is not a function.

If I am separating the chaining of the functions and awaiting each promise it works fine. Is there a way to chain these promises and await them together?

like image 288
Jorayen Avatar asked Sep 01 '26 22:09

Jorayen


1 Answers

You need to await hem both:

const a = new A();
const b = await a.getB();
await b.action();

Or

const a = new A();
await a.getB().then(b => b.action());
like image 140
PVermeer Avatar answered Sep 04 '26 12:09

PVermeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!