当前位置:网站首页>async关键字
async关键字
2022-04-23 14:51:00 【Qwe7】
async关键字
普通函数定义前加async关键字 普通函数变成异步函数
异步函数默认返回promise对象
在异步函数内部使用return关键字进行结果返回 结果会被包裹的promise对象中 return关键字代替了resolve方法
在异步函数内部使用throw关键字抛出程序异常
调用异步函数再链式调用then方法获取异步函数执行结果
调用异步函数再链式调用catch方法获取异步函数执行的错误信息
// 1.在普通函数定义的前面加上async关键字 普通函数就变成了异步函数
// 2.异步函数默认的返回值是promise对象
// 3.在异步函数内部使用throw关键字进行错误的抛出
//
// await关键字
// 1.它只能出现在异步函数中
// 2.await promise 它可以暂停异步函数的执行 等待promise对象返回结果后再向下执行函数
// async function fn () {
// throw '发生了一些错误';
// return 123;
// }
// // console.log(fn ())
// fn ().then(function (data) {
// console.log(data);
// }).catch(function (err){
// console.log(err);
// })
async function p1 () {
return 'p1';
}
async function p2 () {
return 'p2';
}
async function p3 () {
return 'p3';
}
async function run () {
let r1 = await p1()
let r2 = await p2()
let r3 = await p3()
console.log(r1)
console.log(r2)
console.log(r3)
}
run();
await关键字
await关键字只能出现在异步函数中
await promise await后面只能写promise对象 写其他类型的API是不不可以的
await关键字可是暂停异步函数向下执行 直到promise返回结果
const fs = require('fs');
// 改造现有异步函数api 让其返回promise对象 从而支持异步函数语法
const promisify = require('util').promisify;
// 调用promisify方法改造现有异步API 让其返回promise对象
const readFile = promisify(fs.readFile);
async function run () {
let r1 = await readFile('./1.txt', 'utf8')
let r2 = await readFile('./2.txt', 'utf8')
let r3 = await readFile('./3.txt', 'utf8')
console.log(r1)
console.log(r2)
console.log(r3)
}
run();
版权声明
本文为[Qwe7]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1986119
边栏推荐
- MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
- Realization of four data flow modes of grpc based on Multilingual Communication
- Matrix exchange row and column
- We reference My97DatePicker to realize the use of time plug-in
- Contraction mapping theorem
- vscode中文插件不生效问题解决
- 阿里研发三面,面试官一套组合拳让我当场懵逼
- 每日一题-LeetCode396-旋转函数-递推
- Bingbing learning notes: take you step by step to realize the sequence table
- Detailed explanation of C language knowledge points -- first knowledge of C language [1]
猜你喜欢
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
LeetCode149-直线上最多的点数-数学-哈希表
Branch statement of process control
编程哲学——自动加载、依赖注入与控制反转
1 - first knowledge of go language
抑郁症治疗的进展
Leetcode exercise - 396 Rotation function
We reference My97DatePicker to realize the use of time plug-in
LeetCode162-寻找峰值-二分-数组
ASEMI超快恢复二极管与肖特基二极管可以互换吗
随机推荐
First acquaintance with STL
QT Detailed explanation of pro file
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
Bingbing learning notes: take you step by step to realize the sequence table
Unity_ Code mode add binding button click event
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
We reference My97DatePicker to realize the use of time plug-in
we引用My97DatePicker 实现时间插件使用
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
SQL中HAVING和WHERE的区别
电容
Achievements in science and Technology (21)
eolink 如何助力远程办公
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
ASEMI整流模块MDQ100-16在智能开关电源中的作用
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
外包干了四年,废了...
What is the main purpose of PCIe X1 slot?
帧同步 实现