当前位置:网站首页>async await 在map中使用
async await 在map中使用
2022-07-04 19:52:00 【啾比特】
前言
在做微信小程序的练习时,需要遍历数组中的元素,获取各个元素的id并发起请求来请求对应的资源。这就需要将map循环遍历与async await 进行结合。由于是异步操作,async函数返回的是一个promise对象,则map遍历返回得到的是一个promise对象的数组,我们需要等到获取到所有promise之后在进行后续操作。
Promise.all
Promise.all可以将多个Promise实例包装成一个新的Promise实例。同时,成功和失败的返回值是不同的,成功的时候返回的是一个结果数组,而失败的时候则返回最先被reject失败状态的值。
与map结合
异步的一般模式map是Promise.all(arr.map(async (...) => ...))
。
实例(微信小程序中的运用)
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
})
边栏推荐
- uniapp 富文本编辑器使用
- [solution] paddlepaddle 2 X call static graph mode
- Golang中UTF编码和字符集
- In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
- word中插入圖片後,圖片上方有一空行,且删除後布局變亂
- Redis分布式锁的实现
- 《动手学深度学习》(三) -- 卷积神经网络 CNN
- Cdga | six principles that data governance has to adhere to
- word中插入图片后,图片上方有一空行,且删除后布局变乱
- Why is TCP three handshakes and four waves
猜你喜欢
实操自动生成接口自动化测试用例
Jiuqi ny8b062d MCU specification /datasheet
hash 表的概念及应用
What are the functional modules of RFID warehouse management system solution
[server data recovery] a case of RAID5 data recovery stored in a brand of server
[1200. Minimum absolute difference]
多模输入事件分发机制详解
Pytorch---使用Pytorch实现LinkNet进行语义分割
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
随机推荐
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
Go notes (1) go language introduction and characteristics
网络命名空间
LeetCode 8. 字符串转换整数 (atoi)
Go notes (3) usage of go language FMT package
Leetcode+ 81 - 85 monotone stack topic
MySQL statement execution details
Advantages of RFID warehouse management system solution
哈希表、哈希函数、布隆过滤器、一致性哈希
【1200. 最小絕對差】
电脑共享打印机拒绝访问要怎么办
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
Automatic insertion of captions in word
idea大小写快捷键
The concept and application of hash table
acwing 3302. Expression evaluation
Automatic generation of interface automatic test cases by actual operation
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
How does wincc7.5 SP1 find variables and their positions through cross indexing?