-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_await.js
42 lines (36 loc) · 917 Bytes
/
async_await.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function login(username, password) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (username === 'kernelnewbies' && password === '123456') {
resolve('8ef9e41db21905efdefd45241466f9b3')
} else {
reject(new Error('username or password error'))
}
}, 3000)
})
}
function fetchProfile(token) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (token === '8ef9e41db21905efdefd45241466f9b3') {
resolve("{'name':'kernelnewbies','age':18}")
} else {
reject(new Error('token error'))
}
}, 3000)
})
}
async function main() {
var token
var profile
try {
token = await login('kernelnewbies', '123456')
console.log(token)
profile = await fetchProfile(token)
console.log(profile)
} catch (error) {
console.log(error)
}
}
main()
console.log('END')