当前位置:网站首页>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 notes (1) go language introduction and characteristics
- mysql语句执行详解
- 企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
- 记一次重复造轮子(Obsidian 插件设置说明汉化)
- idea插件
- 【申博攻略】六.如何联系心仪的博导
- Play the music of youth
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Android原生数据库的基本使用和升级
- 《动手学深度学习》(三) -- 卷积神经网络 CNN
猜你喜欢

What if the computer page cannot be full screen? The solution of win11 page cannot be full screen

How does wincc7.5 SP1 find variables and their positions through cross indexing?

idea恢复默认快捷键

【1200. 最小绝对差】

What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system

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

强化学习-学习笔记2 | 价值学习

TweenMax表情按钮js特效

What should I do if my computer sharing printer refuses access

How does the computer save web pages to the desktop for use
随机推荐
Record the online bug solving list (unfinished to be continued 7/4)
Why is TCP three handshakes and four waves
【1200. 最小絕對差】
GVM使用
[Shenbo introduction] VI How to contact your favorite doctoral tutor
MySQL - database query - use of aggregate function, aggregate query, grouping query
记一次重复造轮子(Obsidian 插件设置说明汉化)
[1200. Différence absolue minimale]
RFID仓储管理系统解决方案的优点
多模輸入事件分發機制詳解
idea配置标准注释
扩展你的KUBECTL功能
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
Common verification rules of form components -1 (continuously updating ~)
【解决方案】PaddlePaddle 2.x调用静态图模式
Browser render page pass
强化学习-学习笔记2 | 价值学习
idea插件
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