当前位置:网站首页>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
边栏推荐
- A good tool: aardio
- What is the main purpose of PCIe X1 slot?
- 8.4 循环神经网络从零实现
- 1 - first knowledge of go language
- 压缩映射定理
- Leetcode153 - find the minimum value in the rotation sort array - array - binary search
- When splicing HQL, the new field does not appear in the construction method
- Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
- 线程同步、生命周期
- 外包幹了四年,廢了...
猜你喜欢
1-初识Go语言
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
DVWA之暴力破解(Brute Force)Low-->high
8.5 循环神经网络简洁实现
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
抑郁症治疗的进展
MySQL error packet out of order
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
ASEMI三相整流桥和单相整流桥的详细对比
[servlet] detailed explanation of servlet (use + principle)
随机推荐
Vscode Chinese plug-in doesn't work. Problem solving
Outsourcing for four years, abandoned
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
ArrayList collection basic usage
你还不知道责任链模式的使用场景吗?
Select receives both normal data and out of band data
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
GIS数据处理-cesium中模型位置设置
Go basic reflection
8.2 文本预处理
How do I open the win10 startup folder?
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Chapter 7 of JVM series -- bytecode execution engine
分享3个使用工具,在家剪辑5个作品挣了400多
3、 Gradient descent solution θ
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
Programming philosophy - automatic loading, dependency injection and control inversion
go基础 反射
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
Resolve the conflict between computed attribute and input blur event