-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-await.js
62 lines (53 loc) · 1.19 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const employees = [
{
id: 1,
firstName: 'Luke'
},
{
id: 2,
firstName: 'Tom'
},
{
id: 3,
firstName: 'Kim'
}
]
const salaries = [
{
id: 1,
salary: 3000
},
{
id: 2,
salary: 2250
}
]
const getEmployee = (id) => {
return new Promise((resolve, reject) => {
const employee = employees.find((e) => e.id === id)?.firstName;
(employee)
? resolve(employee)
: reject(`Employee with id ${id} is missing`);
})
}
const getSalary = (id) => {
return new Promise((resolve, reject) => {
const salary = salaries.find((s) => s.id === id)?.salary;
(salary)
? resolve(salary)
: reject(`Salary for id ${id} is missing`)
})
}
const getUserInfo = async (id) => {
try {
const employee = await getEmployee(id);
const salary = await getSalary(id);
return `Employee ${employee} has a salary of: ${salary}`;
} catch (error) {
throw error;
}
}
const id = 3;
getUserInfo(id)
.then(data => console.log(data))
.catch(err => console.error('ERROR', err))