当前位置:网站首页>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
})
边栏推荐
- go defer的使用说明
- After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
- PermissionError: [Errno 13] Permission denied: ‘data.csv‘
- Go notes (3) usage of go language FMT package
- TweenMax表情按钮js特效
- Record the online bug solving list (unfinished to be continued 7/4)
- 软件开发过中的采购
- 基于OpenCV haarcascades的对象检测
- MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
- HWiNFO硬件检测工具v7.26绿色版
猜你喜欢

网络命名空间

网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法

Sword finger offer II 80-100 (continuous update)

What are the functional modules of RFID warehouse management system solution

RFID仓储管理系统解决方案的优点

Idea restore default shortcut key

See how Tencent does interface automation testing

colResizable.js自动调整表格宽度插件

6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库

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
随机推荐
企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
TweenMax表情按钮js特效
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
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
Hands on deep learning (III) -- convolutional neural network CNN
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
LeetCode 871. Minimum refueling times
LeetCode 8. String conversion integer (ATOI)
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
acwing 3302. 表达式求值
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
Advantages of semantic tags and block level inline elements
JS卡牌样式倒计时天数
Idea plug-in
D3.js+Three.js数据可视化3d地球js特效
《动手学深度学习》(三) -- 卷积神经网络 CNN