当前位置:网站首页>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
边栏推荐
- 线程同步、生命周期
- Is asemi ultrafast recovery diode interchangeable with Schottky diode
- 科技的成就(二十一)
- QT actual combat: Yunxi chat room
- 【Servlet】Servlet 详解(使用+原理)
- 成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
- Outsourcing for four years, abandoned
- 1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
- Role of asemi rectifier module mdq100-16 in intelligent switching power supply
- 冰冰学习笔记:一步一步带你实现顺序表
猜你喜欢

【Servlet】Servlet 详解(使用+原理)

Role of asemi rectifier module mdq100-16 in intelligent switching power supply

ASEMI超快恢复二极管与肖特基二极管可以互换吗

8.5 循环神经网络简洁实现

Daily question - leetcode396 - rotation function - recursion

Svn detailed use tutorial

Leetcode165 compare version number double pointer string

Explain TCP's three handshakes in detail

capacitance

LeetCode 练习——396. 旋转函数
随机推荐
MySQL报错packet out of order
阿里研发三面,面试官一套组合拳让我当场懵逼
Bingbing learning notes: take you step by step to realize the sequence table
Leetcode151 - invert words in string - String - simulation
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
1n5408-asemi rectifier diode
一款不错的工具:aardio
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
A good tool: aardio
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
Thread synchronization, life cycle
eolink 如何助力遠程辦公
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
DVWA之暴力破解(Brute Force)Low-->high
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
LeetCode167-两数之和II-双指针-二分-数组-查找
Detailed explanation of C language P2 selection branch statement
电容