-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.js
50 lines (47 loc) · 1 KB
/
async.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
43
44
45
46
47
48
49
50
// Callback
asyncFunc1((err, result1) => {
if (err) {
console.log(err);
}
asyncFunc2((err, result2) => {
if (err) {
console.log(err);
}
asyncFunc3((err, result3) => {
if (err) {
console.log(err);
}
}, result2)
}, result3)
})
// Promise
asyncFunc1()
.then(result => {
return asyncFunc2(result)
})
.then(result => {
return asyncFunc3(result)
})
.catch(err => {
console.log(err);
})
// async/await
async function asyncMain() {
try {
const result = await asyncFunc1()
result = await asyncFunc2(result)
result = await asyncFunc3(result)
} catch (e) {
console.error(err);
}
}
export async function list(ctx, next) {
try {
let students = await Student.getAllAsync();
await ctx.render('students/index', {
students: students
})
} catch (err) {
return ctx.api_error(err);
}
}