当前位置:网站首页>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
边栏推荐
- 编程哲学——自动加载、依赖注入与控制反转
- 牛客网数据库SQL实战详细剖析(26-30)
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- 8.2 文本预处理
- ASEMI超快恢复二极管与肖特基二极管可以互换吗
- Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
- Sqlserver transaction and lock problem
- Programming philosophy - automatic loading, dependency injection and control inversion
- ASEMI整流模块MDQ100-16在智能开关电源中的作用
- eolink 如何助力远程办公
猜你喜欢
qt之.pro文件详解
GIS数据处理-cesium中模型位置设置
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
ASEMI超快恢复二极管与肖特基二极管可以互换吗
8.4 循环神经网络从零实现
Find daffodils - for loop practice
《JVM系列》 第七章 -- 字节码执行引擎
外包幹了四年,廢了...
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
1N5408-ASEMI整流二极管1N5408
随机推荐
[jz46 translate numbers into strings]
Select receives both normal data and out of band data
Progress in the treatment of depression
Comment eolink facilite le télétravail
we引用My97DatePicker 实现时间插件使用
8.4 循环神经网络从零实现
牛客网数据库SQL实战详细剖析(26-30)
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
外包幹了四年,廢了...
SVN详细使用教程
1N5408-ASEMI整流二极管1N5408
UML project example -- UML diagram description of tiktok
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
3、 Gradient descent solution θ
vscode中文插件不生效问题解决
Find daffodils - for loop practice
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
每日一题-LeetCode396-旋转函数-递推
Frame synchronization implementation
Outsourcing for four years, abandoned