[英]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()
. A.prototype.getB()
与B.prototype.action()
一样async
。 If I try to await on the chaining of the functions I get an error: TypeError: a.getB(...).action is not a function
. 如果我试图等待函数的链接我得到一个错误:
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?
有没有办法将这些承诺联系在一起并等待它们?
This is because getB
is an async function and does not return a B object but a Promise
object that have no action
method. 这是因为
getB
是一个异步函数,不返回B对象,而是返回没有action
方法的Promise
对象。 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. 此承诺将通过B对象进一步解决,您可以通过使用PVermeer建议的
then
方法捕获它来访问已解析的值。
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.