当前位置:网站首页>Async keyword
Async keyword
2022-04-23 14:52:00 【Qwe7】
async keyword
Add... Before the definition of ordinary function async keyword Ordinary functions become asynchronous functions
Asynchronous functions return... By default promise object
Used inside asynchronous functions return Keyword for result return The result will be wrapped promise In the object return Keywords replace resolve Method
Used inside asynchronous functions throw Keyword throws a program exception
Call asynchronous function and chain call then Method to get the execution result of the asynchronous function
Call asynchronous function and chain call catch Method to get the error information of asynchronous function execution
// 1. Add... Before the ordinary function definition async keyword Ordinary functions become asynchronous functions
// 2. The default return value of an asynchronous function is promise object
// 3. Used inside asynchronous functions throw Keyword to throw an error
//
// await keyword
// 1. It can only appear in asynchronous functions
// 2.await promise It can pause the execution of asynchronous functions wait for promise Object returns the result before executing the function down
// async function fn () {
// throw ' There have been some mistakes ';
// 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 keyword
await Keywords can only appear in asynchronous functions
await promise await You can only write promise object Write about other types of API It's impossible
await Keyword can pause asynchronous function execution until promise Return results
const fs = require('fs');
// Transform existing asynchronous functions api Let it go back to promise object This supports asynchronous function syntax
const promisify = require('util').promisify;
// call promisify Methods to transform the existing asynchronous system API Let it go back to promise object
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://yzsam.com/2022/04/202204231450357655.html
边栏推荐
- epoll 的EPOLLONESHOT 事件———实例程序
- Mds55-16-asemi rectifier module mds55-16
- What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
- LeetCode162-寻找峰值-二分-数组
- Leetcode151 - invert words in string - String - simulation
- LeetCode149-直线上最多的点数-数学-哈希表
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
- OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
- Resolve the conflict between computed attribute and input blur event
- 解决computed属性与input的blur事件冲突问题
猜你喜欢
Chapter 7 of JVM series -- bytecode execution engine
《JVM系列》 第七章 -- 字节码执行引擎
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
每日一题-LeetCode396-旋转函数-递推
ASEMI超快恢复二极管与肖特基二极管可以互换吗
8.3 语言模型与数据集
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
QT interface optimization: QT border removal and form rounding
L'externalisation a duré quatre ans.
【无标题】
随机推荐
【Proteus仿真】自动量程(范围<10V)切换数字电压表
We reference My97DatePicker to realize the use of time plug-in
async关键字
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
The difference between having and where in SQL
PCIe X1 插槽的主要用途是什么?
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
分布式事务Seata介绍
LeetCode167-两数之和II-双指针-二分-数组-查找
Arduino for esp8266串口功能简介
[detailed explanation of factory mode] factory method mode
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
电容
小红书 timestamp2 (2022/04/22)
capacitance
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
Leetcode162 - find peak - dichotomy - array
《JVM系列》 第七章 -- 字节码执行引擎
剑指 Offer II 019. 最多删除一个字符得到回文(简单)