美文网首页
[Observable] 混合async/await

[Observable] 混合async/await

作者: V_Jan | 来源:发表于2020-06-25 22:04 被阅读0次

我想在前端的开发里,避免不了要在某个服务中做回调,而如果回调也是个promise或者observable,那么如果不想用next, err, completed的写法(啰嗦), 想要用async/await 语法糖,那么可以这么写,比如要在next中使用await, 那么 给next 加上async, zai next中就可以用await。

service.createTag({versionNo: this.tagInfo.inputTag}, siteEnvId).subscribe(
            async res => {
                this.tagInfo.code = res.code;
                if (res.code === 200 && res.data) {
                    this.tagInfo.isTagging = false;
                    this.tagInfo.logUrl = res.data.url;
                    await this.autoDiffAfterTag();
                }
            },
            err => {
                this.tagInfo.isTagging = false;
                errorHandler.handle(`Create Tag Error: ${err}`);
            },
            () => {
                this.tagInfo.isTagging = false;
            }
        );

顺带一提,在observable中, next, err, complete只会被执行一次,而在err, 和complete只会有一个被执行。

相关文章

网友评论

      本文标题:[Observable] 混合async/await

      本文链接:https://www.haomeiwen.com/subject/qqyefktx.html