当前位置:网站首页>async-await
async-await
2022-08-03 00:07:00 【cjx177187】
async
作为一个关键字放在函数的前面,表示该函数是一个异步函数,意味着该函数的执行不会阻塞后面代码的执行 异步函数的调用跟普通函数一样
await
await即等待,用于等待一个Promise对象。它只能在异步函数 async function中使用,否则会报错
它的返回值不是Promise对象而是Promise对象处理之后的结果
await表达式会暂停当前 async function的执行,等待Promise 处理完成。若 Promise 正常处理(fulfilled),其回调的resolve函数参数作为 await 表达式的值,继续执行 async function,若 Promise 处理异常(rejected),await 表达式会把 Promise 的异常原因抛出。如果 await 操作符后的表达式的值不是一个 Promise,那么该值将被转换为一个已正常处理的 Promise。
使用方法举例:

index.js文件
var router=require("./router.js")
router.get("/home1",(req,res)=>{
res.end('{"info":"hello1"}')
})
router.get("/home2",(req,res)=>{
res.end('{"info":"hello2"}')
})
router.get("/home3",(req,res)=>{
res.end('{"info":"hello3"}')
})
router.js文件
var fs=require("fs")
var url=require("url")
var querysting=require("querystring")
var mime=require("mime")
let urls={}
var http=require("http")
let router=function(req,res){
//这个函数每次用户访问时运行
let pathname=url.parse(req.url).pathname
fs.readFile(router.basepath+pathname,(err,data)=>{
if(!err){
res.setHeader("content-Type",mime.getType(pathname))
res.end(data)
}else{
if(!urls[pathname]){ res.end("404 not found-mymvc")}
else{ urls[pathname](req,res)}
}
})
}
router.static=function(path){
this.basepath=path
}
router.get=function(url,cb){
urls[url]=cb
}
router.basepath=__dirname+"/public"
http.createServer(router).listen(8080)
module.exports=router;
index.html文件:
<!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>
<script src='https://s1.pstatp.com/cdn/expire-1-M/axios/0.19.2/axios.js'></script>
</head>
<body>
<button onclick="fn()">点击显示数据</button>
<script>
async function fn(){
let data1=await axios('/home1')
console.log(data1)
let data2=await axios('/home2')
console.log(data2)
let data3=await axios('/home3')
console.log(data3)
}
</script>
</body>
</html>
运行代码;

边栏推荐
- HVV红队 | 渗透测试思路整理
- js显示隐藏手机号
- GTK实现水波纹效果
- matplotlib中的3D绘图警告解决:MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure
- js基础知识整理之 —— 字符串
- UPC2022暑期个人训练赛第23场(Credit Card Payment)
- Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
- letcode 第20题-有效的括号
- Speech Synthesis Model Cheat Sheet (1)
- 【QT】自定义工程封装成DLL并如何调用(带ui界面的)
猜你喜欢

【图像分类】2022-MPViT CVPR

优秀论文以及思路分析02

并发模型和I/O模型介绍

Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead

德邦科技通过注册:年营收5.8亿 国家集成电路基金为大股东

HVV红队 | 渗透测试思路整理

Connect the Snowflake of CKAN tutorial CKAN to release to open data portal

esp32和ros2基础篇草稿-micro-ros-

2022 开放原子全球开源峰会 | 麒麟信安携手openEuler助力开源产业繁荣发展

MySQL的多表查询(1)
随机推荐
基于rt-thread studio的STM32裸机开发——LED
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
Oracle 暴跌,倒下了!
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
【Autosar RTM】
ORA-55610: Invalid DDL statement on history-tracked table
别再到处乱放配置文件了!我司使用 7 年的这套解决方案,稳的一秕
2149. 按符号重排数组
alibaba数据同步组件canal的实践整理
我为什么又能面试一次就拿到offer
解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
C# 异步编程(async和await)
Understand the next hop address in the network topology in seconds
【TypeScript笔记】01 - TS初体验 && TS常用类型
random.nextint()详解
通力传动递交注册:年营收4.7亿 实控人项献忠家族色彩浓厚
Merge two excel spreadsheet tools
九零后程序员心声:互联网的同行们,别卷了,再卷人都卷没了
【图像分类】2022-MPViT CVPR
UE5 官方案例Lyra 全特性详解 8.如何用配置表初始化角色数据