-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpromise.js
More file actions
55 lines (47 loc) · 1.25 KB
/
promise.js
File metadata and controls
55 lines (47 loc) · 1.25 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
function somePromise(someObject) {
console.log("Start work with promise..");
let promise = new Promise(function(resolve, reject) {
setTimeout(function() {
let random = Math.random();
if (random > 0.3) {
someObject.prop2 = 11;
resolve(someObject);
}
reject("Some error.");
}, 1000);
});
return promise;
}
function firstFunc(someObject) {
console.log("First function.");
console.log(someObject);
someObject.prop3 = "This is first func prop.";
return new Promise((resolve, reject) => {
resolve(someObject)
});
}
function secondFunc(someObject) {
console.log("Second function.");
console.log(someObject);
return Promise.resolve({ propNew: 111 })
}
function thirdFunc(someObject) {
console.log("Third function.");
console.log(someObject);
return new Promise((resolve, reject) => {
reject("Third func error.");
});
}
function finallyFunc() {
console.log("End work with promise.")
}
const testObject = {
prop1: "Property one",
prop2: 1
}
somePromise(testObject)
.then(firstFunc)
.then(secondFunc)
.then(thirdFunc)
.catch(error => console.error(error))
.finally(finallyFunc);