当前位置:网站首页>Async await used in map
Async await used in map
2022-07-04 22:55:00 【Chirp bit】
Preface
When practicing wechat applet , You need to traverse the elements in the array , Get the id And send a request to request the corresponding resource . This requires that map Loop traversal and async await Combine . Because of asynchronous operation ,async The function returns a promise object , be map Traversal returns a promise An array of objects , We need to wait until we get all promise After that, follow-up operations .
Promise.all
Promise.all Multiple Promise The example is packaged into a new Promise example . meanwhile , Success and failure return values are different , Success returns an array of results , And when they fail, they return to be the first reject Value of failure state .
And map combination
The general pattern of asynchrony map yes Promise.all(arr.map(async (...) => ...))
.
example ( Application of wechat applet )
let videos = video.datas.map(async item =>{
item.id = index++;
item.videoUrl = await this.getVideoUrl(item.data.vid)
return item
})
let temp = await Promise.all(videos)
this.setData({
video:temp
})
边栏推荐
- Logo special training camp section II collocation relationship between words and graphics
- Redis入门完整教程:键管理
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
- Logo Camp d'entraînement section 3 techniques créatives initiales
- How to manage 15million employees easily?
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- 蓝队攻防演练中的三段作战
- 特征缩放 标准化 归一化
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- Redis入门完整教程:Bitmaps
猜你喜欢
Serial port data frame
Li Kou 98: verify binary search tree
浅聊一下中间件
Advanced area of attack and defense world misc 3-11
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Redis入门完整教程:GEO
Redis入门完整教程:有序集合详解
Hit the core in the advanced area of misc in the attack and defense world
PMO: compare the sample efficiency of 25 molecular optimization methods
随机推荐
leetcode 72. Edit distance edit distance (medium)
蓝队攻防演练中的三段作战
Redis introduction complete tutorial: slow query analysis
Sword finger offer 68 - I. nearest common ancestor of binary search tree
Mongodb aggregation operation summary
SPSS安装激活教程(包含网盘链接)
[roommate learned to use Bi report data processing in the time of King glory in one game]
Sword finger offer 68 - ii The nearest common ancestor of binary tree
特征缩放 标准化 归一化
String类中的常用方法
Locust performance test - environment construction and use
Why is Dameng data called the "first share" of domestic databases?
华泰证券是国家认可的券商吗?开户安不安全?
业务太忙,真的是没时间搞自动化理由吗?
Attack and defense world misc master advanced zone 001 normal_ png
繁华落尽、物是人非:个人站长该何去何从
Redis入门完整教程:Redis使用场景
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
UML图记忆技巧
[cooking record] - stir fried 1000 pieces of green pepper