首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写async await Angular示例

编写async await Angular示例
EN

Stack Overflow用户
提问于 2019-02-18 01:24:33
回答 1查看 552关注 0票数 1

我不明白为什么我创建的async/await函数没有等待。

代码语言:javascript
复制
async mustWait() {
    this.response = await this.service.mustBePrintedFirst();
    console.log('I will wait');
}

mustBePrintedFirst() {
    http.get(.....);
    console.log('I must be printed first');
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-18 01:46:42

像下面这样返回一个承诺。

代码语言:javascript
复制
mustBePrintedFirst() {
    return new Promise(resolve => {
    resolve('whatever you want to resolve');
    console.log('I must be printed first');
   });
}

在你的情况下,它将是

代码语言:javascript
复制
mustBePrintedFirst() {
    http.get(...).toPromise() // something ike this
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54735744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档