当前位置:网站首页>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
})
边栏推荐
- How to send a reliable request before closing the page
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
- A complete tutorial for getting started with redis: Pipeline
- 都说软件测试很简单有手就行,但为何仍有这么多劝退的?
- The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
- The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
- Redis入门完整教程:事务与Lua
- Logo special training camp section III initial creative techniques
- 繁華落盡、物是人非:個人站長該何去何從
- Advanced area of attack and defense world misc 3-11
猜你喜欢
随机推荐
【lua】int64的支持
10 schemes to ensure interface data security
Redis入门完整教程:慢查询分析
剑指Offer 68 - II. 二叉树的最近公共祖先
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Li Kou 98: verify binary search tree
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Detailed explanation of heap sort code
md5工具类
SPSS安装激活教程(包含网盘链接)
Redis入门完整教程:有序集合详解
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
9 - 类
Advanced area of attack and defense world misc 3-11
Hit the core in the advanced area of misc in the attack and defense world
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
关于栈区、堆区、全局区、文字常量区、程序代码区
蓝队攻防演练中的三段作战