当前位置:网站首页>JS promise, async, await simple notes
JS promise, async, await simple notes
2022-06-11 05:17:00 【kankan231】
promise demo
function getToken(username,pwd) {
console.log("111")
return new Promise(function (resolve, reject) {
console.log("222")
setTimeout(() => {
console.log("333")
if (username=="admin" && pwd=="123456"){
resolve("xxx")
}else{
reject(" Wrong user name or password ")
}
}, 1000);
})
}
console.log("444")
getToken("admin","123456").then(function (token) {
console.log("token:",token)
}).catch(function (error) {
console.log("error:",error)
})
console.log("555")Output :
444
111
222
555
333
token: xxx
It can be seen from the results ,promise function The code in is executed synchronously , When calling resolve It will callback then, It will also pass parameters to then, call reject It will callback catch, It will also pass parameters to catch
async/await demo
function getToken(username,pwd) {
console.log("111")
return new Promise(function (resolve, reject) {
console.log("222")
setTimeout(() => {
console.log("333")
if (username=="admin" && pwd=="123456"){
resolve("xxx")
}else{
reject(" Wrong user name or password ")
}
}, 1000);
})
}
async function asyncTest(){
console.log("444")
try {
let token = await getToken("admin","123456")
console.log("token:",token)
}catch (e) {
console.log("err:",e)
}
console.log("555")
}
let ret = asyncTest();
console.log("ret:",ret)Output :
444
111
222
ret: Promise { <state>: "pending" }
333
token: xxx
555
analysis : When used in a function await When a keyword , Function must be preceded by async keyword ,await The function of keywords is , When await The value of the following expression is a number / String and other common types ,await Get this value directly , When the value of the expression is promise Object time , Then wait for the promise object resolve or reject,await Will get resolve or reject Value , there try-catch It's used to deal with reject The situation of . It can be seen from the results ,async The function always automatically returns a promise object , When executed await When the sentence is , The function returns , etc. await The end will automatically execute the following remaining code
边栏推荐
- BP neural network derivation + Example
- Huawei equipment is configured with bgp/mpls IP virtual private network
- Zed2 camera manual
- Cascade EF gan: local focus progressive facial expression editing
- Network security construction in 5g Era
- [opencv learning problems] 1 Namedwindow() and imshow() show two windows in the picture
- 35.搜索插入位置
- Let me tell you how to choose a 10 Gigabit network card
- Section II: structural composition characteristics of asphalt pavement (2) structural layer and performance requirements
- New library goes online | cnopendata immovable cultural relic data
猜你喜欢

oh my zsh正确安装姿势

Lr-link Lianrui fully understands the server network card

Simple knowledge distillation

The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future

AAAI2022-ShiftVIT: When Shift Operation Meets Vision Transformer

Huawei equipment configures local virtual private network mutual access

JVM tuning V: JVM tuning tools and tuning practice

Electrolytic solution for ThinkPad X1 carbon battery

Paper reproduction: pare

Combien de courant le câblage des PCB peut - il supporter?
随机推荐
Sealem finance builds Web3 decentralized financial platform infrastructure
Solving graph problems with union search set
Opencv learning path (2-5) -- Deep parsing imwrite function
How to apply for free idea with official documents
Target detection - personal understanding of RCNN series
Preliminary understanding of DFS and BFS
Topological sorting
Simple knowledge distillation
About custom comparison methods of classes and custom methods of sort functions
Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
Restoration of binary tree -- number restoration
Opencv learning path (2-2) -- Deep parsing namedwindow function
自定义View之基础篇
The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future
使用acme.sh自动申请免费SSL证书
wxParse解析iframe播放视频
Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
New product release: Lianrui launched a dual port 10 Gigabit bypass network card
Leetcode 161 Editing distance of 1 (2022.06.10)
Let me tell you how to choose a 10 Gigabit network card