当前位置:网站首页>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
边栏推荐
- Find daffodils - for loop practice
- Brute force of DVWA low -- > High
- thinkphp5+数据大屏展示效果
- [jz46 translate numbers into strings]
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- The art of automation
- 分享3个使用工具,在家剪辑5个作品挣了400多
- 中富金石财富班29800效果如何?与专业投资者同行让投资更简单
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
- 电容
猜你喜欢

QT actual combat: Yunxi calendar

Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems

Leetcode153 - find the minimum value in the rotation sort array - array - binary search

Brute force of DVWA low -- > High

8.5 循环神经网络简洁实现

Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?

1N5408-ASEMI整流二极管1N5408

Svn detailed use tutorial

《JVM系列》 第七章 -- 字节码执行引擎
![[detailed explanation of factory mode] factory method mode](/img/56/04fa84d0b5f30e759854a39afacff2.png)
[detailed explanation of factory mode] factory method mode
随机推荐
脏读、不可重复读和幻读介绍
QT actual combat: Yunxi chat room
Chapter 7 of JVM series -- bytecode execution engine
3、 Gradient descent solution θ
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
pnpm安装使用
epoll 的 ET,LT工作模式———实例程序
Thread synchronization, life cycle
Unity_ Code mode add binding button click event
Comment eolink facilite le télétravail
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
PCIe X1 插槽的主要用途是什么?
你还不知道责任链模式的使用场景吗?
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
【Proteus仿真】自动量程(范围<10V)切换数字电压表
QT interface optimization: QT border removal and form rounding
Progress in the treatment of depression
Epolloneshot event of epoll -- instance program
1 - first knowledge of go language