Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

async和promise 执行问题

image.png

image.png

上图是函数定义,下图是调用
红框处两个函数 调用的时候,得到的结果不一样。

大家帮忙看下红框处两个函数哪里有区别。感谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

不贴代码,我很难给你做事呀??
原因是await不是智能的,它并不知道你是想在successresolve
换言之,对于带异步过程而不返回Promise的函数而言,你必须得像第二段代码那样用Promise包装返回值、并指明在何处resolve,然后才能愉快地使用await
这样看来,似乎用await还要更麻烦一些,但是返回Promise的接口越来越多了,遇见一次包装一次就行了。
爪机打字太麻烦了,多多担待。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...