-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpromise.js
More file actions
66 lines (60 loc) · 1.64 KB
/
promise.js
File metadata and controls
66 lines (60 loc) · 1.64 KB
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
63
64
65
66
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getStudent(resolve, reject) {
const time = randomInteger(1, 2) * 1000;
console.log("getStudent", time);
setTimeout(function () {
resolve([
{ name: "Dupont", cours: [1, 3, 5] },
{ name: "Lea", cours: [2, 4] },
{ name: "Charles", cours: [1] },
]);
}, time);
}
function getCourses(resolve, reject) {
const time = randomInteger(2, 4) * 1000;
console.log("getCourses", time);
setTimeout(function () {
resolve([
{ id: 1, name: "JS" },
{ id: 2, name: "PHP" },
{ id: 3, name: "C#" },
{ id: 4, name: "F#" },
{ id: 5, name: "CSS" },
]);
}, time);
}
function mapping(resolve, reject) {
const time = randomInteger(1, 4) * 1000;
console.log("mapping", time);
setTimeout(function () {
Promise.all([new Promise(getStudent), new Promise(getCourses)]).then(
function (results) {
const students = results[0];
const courses = results[1];
const mappedStudents = students.map(function (student) {
student.cours = student.cours.map(function (cours) {
return courses.find(function (course) {
return course.id === cours;
});
});
return student;
});
resolve(mappedStudents);
}
);
}, time);
}
const timer = function (resolve, reject) {
setTimeout(function () {
reject();
}, 5000);
};
Promise.race([new Promise(mapping), new Promise(timer)])
.then(function (result) {
console.log("Merge OK");
})
.catch(function (reject) {
console.log("Timeout");
});