当前位置:网站首页>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
})
边栏推荐
- [solution] paddlepaddle 2 X call static graph mode
- 伦敦银走势图分析的新方法
- Win11无法将值写入注册表项如何解决?
- How does the computer save web pages to the desktop for use
- 记录线上bug解决list(未完待续7/4)
- 【解决方案】PaddlePaddle 2.x调用静态图模式
- hash 表的概念及应用
- Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
- 分析伦敦银走势图的技巧
- Idea case shortcut
猜你喜欢
Sword finger offer II 80-100 (continuous update)
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
实操自动生成接口自动化测试用例
Leetcode+ 81 - 85 monotone stack topic
What should I do if my computer sharing printer refuses access
What if win11u disk refuses access? An effective solution to win11u disk access denial
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
render函数与虚拟dom
电脑共享打印机拒绝访问要怎么办
随机推荐
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
hash 表的概念及应用
【1200. 最小絕對差】
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
[solution] paddlepaddle 2 X call static graph mode
JS卡牌样式倒计时天数
mysql语句执行详解
LeetCode 8. 字符串转换整数 (atoi)
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
How to solve the problem that win11 cannot write the value to the registry key?
测试员的算法面试题-找众数
GVM使用
jekins初始化密码没有或找不到
Idea case shortcut
实操自动生成接口自动化测试用例
强化学习-学习笔记2 | 价值学习
word中使用自动插入题注功能
Qt五子棋人机对战画棋子之QPainter的使用误区总结
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法