当前位置:网站首页>遍历await方法的区别:以for和forEach为例
遍历await方法的区别:以for和forEach为例
2022-08-04 21:47:00 【新生代农民工官方认证码农小拽】
场景举例
先简单了解一下遍历await的应用场景,有助于增进对遍历方法区别的了解。
比如我有一组学生名单
const studentNames = ['dilireba', 'zhangsan', 'xiaoming']
现在需要根据学生姓名到获取到数据库中对应的学生信息,
数据库查询是异步的,
此时就需要用到遍历await来查询
方法介绍
遍历方法目前有四个比较常见的:
forfor ... of ...forEach重写forEach
百度可自行查询详细介绍。
区别
在此处以for和 forEach为例。
先从两者的遍历结构说起:
for:
const getStudentDetails = async () {
for(let i = 0; i < studentNames.length; i++) {
let s = await mongodb.find(...)
}
}
forEach:
const getStudentDetails = async () => {
studentNames.forEach(async item => {
let s = await mongodb.find(...)
})
}
可以看到,for属于同步操作,而forEach中的遍历过程是在回调函数中的,
也就是异步的。
很显然, for在遍历后,可以直接得出想要的结果,而forEach是不行的。
举个例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script> function test() {
return new Promise((resolve, reject) => {
resolve(111) }) } const arr = [1, 2, 3] const arr1 = [] const arr2 = [] async function fn1() {
for(let i = 0; i < arr.length; i++) {
let r = await test() arr1.push(r) } // --> 这里打印的结果,就是想要得到的结果 console.log('fn1-arr1: ', arr1) } fn1() async function fn2() {
arr.forEach(async item => {
let r = await test() arr2.push(r) console.log('arr2: ', arr2) }) // --> 这里打印的arr2是[] console.log('fn2-arr2: ', arr2) } fn2() </script>
</body>
</html>
边栏推荐
- 【SQL之降龙十八掌】01——亢龙有悔:入门10题
- OD-Model [6]: YOLOv2
- 1319_STM32F103串口BootLoader移植
- "Jianzhi offer" brush title classification
- 开发deepstram的自定义插件,使用gst-dseaxmple插件进行扩充,实现deepstream图像输出前的预处理,实现图像自定义绘制图(精四)
- 8 年产品经验,我总结了这些持续高效研发实践经验 · 协同篇
- 驱动点云格式修改带来的效率提升
- 数字重塑客观世界,全空间GIS发展正当其时
- DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
- NFT宝典:你需要知道NFT的术语和定义
猜你喜欢

Spss-系统聚类手算实操

如何一键重装win7系统?重装win7系统详细教程

ROS播包可视化
![[larave]关于laravel使用form submit()不能获取值问题](/img/2f/a7a17b1497390509edd6d096e8e473.png)
[larave]关于laravel使用form submit()不能获取值问题

Develop your own text recognition application with Tesseract

How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
![[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication](/img/38/764b447cf7d886500a9b99d7679cb6.png)
[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication

如何将二叉搜索树转化为一个有序的双向链表(原树上修改)

DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂

DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-Polyethylene Glycol-Aldehyde A hydrophobic 18-carbon phospholipid
随机推荐
数字重塑客观世界,全空间GIS发展正当其时
rk3399-0.0 svc命令
数电快速入门(四)(组合逻辑电路的分析以及设计的介绍)
Altium Designer 19.1.18 - draw polygons copper hollow out, for the cursor just capture solutions
input事件中文触发多次问题研究php DEBUG
AXI interface application of Zynq Fpga image processing - the use of axi_lite interface
【uiautomation】微信好友列表获取(存储到txt中)
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
The upgrade and transformation plan of the fortress machine for medium and large commercial banks!Must see!
buu web
win10 uwp use WinDbg to debug
In action: 10 ways to implement delayed tasks, with code!
Excel商业智能-Power BI电商数据分析实战
boostrap多选PID查找端口 window
【SQL之降龙十八掌】01——亢龙有悔:入门10题
LeetCode: 406. 根据身高重建队列
如何为Web3.0世界启动完美的DAO
数电快速入门(三)(卡诺图化简法的介绍)
[Linear Algebra 02] 2 interpretations of AX=b and 5 perspectives of matrix multiplication
Yolov7:Trainable bag-of-freebies sets new state-of-the-art for real-time objectdetectors