我想在前端的开发里,避免不了要在某个服务中做回调,而如果回调也是个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只会有一个被执行。
网友评论