javascript等待多个链式异步函数 [英] javascript await on multiple chained async functions

查看:266
本文介绍了javascript等待多个链式异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下内容:

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

A.prototype.getB()B.prototype.action()一样是async. 如果我尝试等待函数的链接,则会收到错误消息: TypeError: a.getB(...).action is not a function.

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?

推荐答案

这是因为getB是异步函数,不会返回B对象,而是没有action方法的Promise对象.该承诺将进一步用B对象解决,您可以通过使用PVermeer提出的then方法捕获已解决的值来访问已解决的值.

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.

这篇关于javascript等待多个链式异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆