当前位置:网站首页>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
})
边栏推荐
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
- 【1200. 最小絕對差】
- LeetCode+ 81 - 85 单调栈专题
- vim异步问题
- Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
- word中使用自动插入题注功能
- Qt五子棋人机对战画棋子之QPainter的使用误区总结
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- uniapp 富文本编辑器使用
- 二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
猜你喜欢

伦敦银走势图分析的新方法

From automation to digital twins, what can Tupo do?

WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?

How to solve the problem that win11 cannot write the value to the registry key?

看腾讯大老如何做接口自动化测试

多模输入事件分发机制详解

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

HWiNFO硬件检测工具v7.26绿色版
![[1200. Différence absolue minimale]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Différence absolue minimale]

二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
随机推荐
实操自动生成接口自动化测试用例
Idea case shortcut
软件开发过中的采购
Vue cleans up the keepalive cache scheme in a timely manner
Sword finger offer II 80-100 (continuous update)
word中使用自动插入题注功能
电脑怎么保存网页到桌面上使用
网络命名空间
vim异步问题
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
LeetCode 7. 整数反转
伦敦银走势图分析的新方法
How does win11 search for wireless displays? Win11 method of finding wireless display device
LeetCode 7. Integer inversion
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
接口设计时的一些建议
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
字节测试工程师十年经验直击UI 自动化测试痛点
js 3D爆炸碎片图片切换js特效
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system