-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromise-Ex1.js
61 lines (47 loc) · 1.28 KB
/
Promise-Ex1.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
let employees =[{
id: 1,
name: 'Linux Torvards',
},{
id: 2,
name: 'Bill Gates',
},{
id: 3,
name: 'Jeff Bezos',
}];
let salaries = [{
id: 1,
salary: 4000,
},{
id: 2,
salary: 1000,
},{
id: 3,
salary: 2000,
}];
const getEmpleado = (id) => {
return new Promise((resolve, reject) => {
if(employees.find(employee => employee.id === id)){ //parametro y la condicion que queremos que cuumpla y parámetro id porque es lo que queremos que varíe
resolve(employees[id-1]); //para que respete las posiciones de los [indices]que empieza por 0
} else{
reject('Employee not found');
}
});
};
const getSalario = (id) => {
return new Promise((resolve, reject) => {
if(salaries.find(employee => employee.id === id)){
resolve("Employee n" + id + " "+ salaries[id-1].salary);
}else{
reject("Employee salary not defined");
}
});
};
getEmpleado(1)
.then((employee) => {
console.log('Employee found '+ "n" +employee.id+ " "+ employee.name);
return getSalario(employee.id);//aqui el parámetro recibe un objeto employee para que actue de acuerdo
})
.then(salary => {
console.log(salary); //encadenamos promesas
})
.catch(error => console.error(error));