当前位置:网站首页>js promise,async,await简单笔记
js promise,async,await简单笔记
2022-06-11 05:09: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("用户名或密码错误")
}
}, 1000);
})
}
console.log("444")
getToken("admin","123456").then(function (token) {
console.log("token:",token)
}).catch(function (error) {
console.log("error:",error)
})
console.log("555")输出:
444
111
222
555
333
token: xxx
从结果可以看出,promise function中的代码是同步执行的,当调用resolve时会回调then,同时也会把参数传递给then,调用reject时会回调catch,同时也会把参数传递给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("用户名或密码错误")
}
}, 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)输出:
444
111
222
ret: Promise { <state>: "pending" }
333
token: xxx
555
分析:当在函数中使用await关键字时,函数前必须加async关键字,await关键字的作用是,当await后面的表达式的值为数字/字符串等普通类型时,await直接得到这个值,当表达式的值为promise对象时,则等待该promise对象resolve或reject,await将得到resolve或reject的值,这里的try-catch是用来处理reject的情况。从结果可以看出,async函数始终会自动返回一个promise对象,当执行到await语句时,函数就返回了,等await结束会自动往下执行下面剩余的代码
边栏推荐
- Linked list de duplication
- Paper reproduction: pare
- Sealem Finance打造Web3去中心化金融平台基础设施
- 华为设备配置通过GRE接入虚拟专用网
- Poverty has nothing to do with suffering
- About custom comparison methods of classes and custom methods of sort functions
- Zed2 camera calibration -- binocular, IMU, joint calibration
- Google drive download failed, network error
- New library goes online | cnopendata immovable cultural relic data
- Huawei equipment is configured with bgp/mpls IP virtual private network
猜你喜欢

Simple knowledge distillation

Cartographer learning record: cartographer Map 3D visualization configuration (self recording dataset version)

oh my zsh正确安装姿势

Reverse thinking: making cartoon photos real

Apply the intelligent OCR identification technology of Shenzhen Yanchang technology to break through the bottleneck of medical bill identification at one stroke. Efficient claim settlement is not a dr

华为设备配置MCE

2021 iccv paper sharing - occlusion boundary detection

Overview of self attention acceleration methods: Issa, CCNET, cgnl, linformer

Click the icon is not sensitive how to adjust?

JVM tuning 6: GC log analysis and constant pool explanation
随机推荐
Zed2 running vins-mono preliminary test
QT Road (1) -- Introduction to pro file
Inventory | ICLR 2022 migration learning, visual transformer article summary
Parametric contractual learning: comparative learning in long tail problems
Some details about memory
选择数字资产托管人时,要问的 6 个问题
Leetcode 161 Editing distance of 1 (2022.06.10)
JVM tuning V: JVM tuning tools and tuning practice
Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
New product pre-sale: 25g optical network card based on Intel 800 series is coming
课程设计总结
New library goes online | cnopendata immovable cultural relic data
ROS compilation error: could not find a package configuration file provided by "XXX“
Ican uses fast r-cnn to get an empty object detection result file
【Markdown语法高级】 让你的博客更精彩(三:常用图标模板)
Huawei equipment configures local virtual private network mutual access
What is the difference between a wired network card and a wireless network card?
Reverse thinking: making cartoon photos real
KD-Tree and LSH
Section I: classification and classification of urban roads