当前位置:网站首页>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>
运行代码;

边栏推荐
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- alibaba数据同步组件canal的实践整理
- random.nextint()详解
- C# 异步编程(async和await)
- What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
- 数据库审计 - 网络安全的重要组成部分
- 公司招个程序员,34岁以上两年一跳的不要,开出工资以为看错了
- Visual Studio中vim模拟器
- Jenkins汉化设置
- Database auditing - an essential part of network security
猜你喜欢
随机推荐
UPC2022暑期个人训练赛第23场(Credit Card Payment)
Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
有奖提问|《新程序员》专访“Apache之父”Brian Behlendorf
letcode 第20题-有效的括号
fifa将采用半自动越位技术计算进球
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
[NCTF2019]SQLi-1||SQL注入
麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
智能合约安全-可重入攻击(SW107-Reentrancy)
What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
Nacos配置中心之事件订阅
Understand the next hop address in the network topology in seconds
并发模型和I/O模型介绍
并查集总结
定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!
Jenkins汉化设置
js基础知识整理之 —— 全局作用域
7.31
Let's talk about the charm of code language









