Skip to content Skip to sidebar Skip to footer

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 functio

Solution 1:

This is because getB is an async function and does not return a B object but a Promise object that have no action method. This promise will further be resolved with a B object and you can access to the resolved value by catching it with the then method as proposed by PVermeer.

Solution 2:

You need to await hem both:

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

Or

const a = newA();
await a.getB().then(b => b.action());

Post a Comment for "Javascript Await On Multiple Chained Async Functions"