当前位置:网站首页>async await如何实现并发
async await如何实现并发
2022-07-28 11:10:00 【卷王出征】
还记得去年实习参加秋招,二面面试官龙哥(现在的组长)问了一个面试题,async await如何实现并发?今晚突然又想起来,赶紧整理一下。
举例
var fs=require('fs');
var read=function (path) {
return new Promise((resolve,reject)=>{
fs.readFile(path,(err,data)=>{
if(err){
reject(err)
}else{
resolve(data)
}
})
})
}
//继发读取
async function ReadTwo(){
var f1=await read('./a.txt');
var f2=await read('./b.txt');
}
解决方案
(1) promise.all()
async function readTwo{
var p=Promise.all([read('./a.txt'),read('./b.txt')]);
var [f1,f2]=await p
}
因为promise.all是并发,所以这两个请求真正达到并发
(2)没法描述,看代码
async function readTwo{
var p1=read('./a.txt');
var p2=read('./a.txt');
f1=await p1;
f2=await p2;
}
这两个请求真的是并发,因为它们是异步函数,但是获取结果后的赋值时继发
现在想想好简单,当时都不会。
边栏推荐
- 保障邮箱安全,验证码四个优势
- 「以云为核,无感极速」第五代验证码重磅来袭
- Object to object mapping -automapper
- Database advanced learning notes -- object type
- MySQL (version 8.0.16) command and description
- "Node learning notes" koa framework learning
- Several reincarnation stories
- CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总
- WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications
- What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
猜你喜欢

What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
![完整版H5社交聊天平台源码[完整数据库+完整文档教程]](/img/3f/03239c1b4d6906766348d545a4f234.png)
完整版H5社交聊天平台源码[完整数据库+完整文档教程]

Design and implementation of SSM personal blog system

Xiaoshuidi 2.0 website navigation network template

Techniques for visualizing large time series.

Ripro9.0 revised and upgraded version +wp two beautification packages + rare plug-ins

天狼星网络验证源码/官方正版/内附搭建教程
![Full version of H5 social chat platform source code [complete database + complete document tutorial]](/img/3f/03239c1b4d6906766348d545a4f234.png)
Full version of H5 social chat platform source code [complete database + complete document tutorial]

Game theory 1. Introduction (basic concepts of combination games, confrontation search, bash games, Nim games)

A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
随机推荐
保障邮箱安全,验证码四个优势
R language - some metrics for unbalanced data sets
Are interviews all about memorizing answers?
Solutions to slow start of MATLAB
Installing sqlmap on win10 (Windows 7)
MySQL离线同步到odps的时候 可以配置动态分区吗
AlexNet—论文分析及复现
完整版H5社交聊天平台源码[完整数据库+完整文档教程]
Using C language to compile student achievement management system (C language student achievement management system deleted)
CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总
Flutter tutorial flutter navigator 2.0 with gorouter, use go_ Router package learn about the declarative routing mechanism in fluent (tutorial includes source code)
Install SSL Certificate in Litespeed web server
Outlook suddenly becomes very slow and too laggy. How to solve it
大佬们,问下,这个不能checkpoint,因为有个jdbc的task任务状态是FINISHED,
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
[cesium] entity property and timing binding: the sampledproperty method is simple to use
Good use explosion! The idea version of postman has been released, and its functions are really powerful
STM32 drives st7701s chip (V ⅰ V0 mobile phone screen change price)
Full version of H5 social chat platform source code [complete database + complete document tutorial]
PFP会是数字藏品的未来吗?