当前位置:网站首页>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
边栏推荐
- Role of asemi rectifier module mdq100-16 in intelligent switching power supply
- 每日一题-LeetCode396-旋转函数-递推
- 8.5 循环神经网络简洁实现
- Leetcode151 - invert words in string - String - simulation
- I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
- How do I open the win10 startup folder?
- MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
- Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
- [jz46 translate numbers into strings]
- 在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
猜你喜欢
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
A good tool: aardio
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
Want to be an architect? Tamping the foundation is the most important
Leetcode exercise - 396 Rotation function
Progress in the treatment of depression
thinkphp5+数据大屏展示效果
每日一题-LeetCode396-旋转函数-递推
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
随机推荐
8.3 语言模型与数据集
ASEMI超快恢复二极管与肖特基二极管可以互换吗
[NLP] HMM hidden Markov + Viterbi word segmentation
DVWA之暴力破解(Brute Force)Low-->high
We reference My97DatePicker to realize the use of time plug-in
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Frame synchronization implementation
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
Realization of four data flow modes of grpc based on Multilingual Communication
【工厂模式详解】工厂方法模式
Epoll's et, lt working mode -- example program
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Unity_ Code mode add binding button click event
Vscode Chinese plug-in doesn't work. Problem solving
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
LeetCode149-直线上最多的点数-数学-哈希表
What is the main purpose of PCIe X1 slot?
pnpm安装使用
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Leetcode exercise - 396 Rotation function