-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromises-experiment.js
More file actions
43 lines (33 loc) · 982 Bytes
/
promises-experiment.js
File metadata and controls
43 lines (33 loc) · 982 Bytes
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
//Working on Promises
//Additional Information
/*
setTimeout(function|code, delay(ms), argsfordeclaredfns.[],argsfordeclaredfns.[])
*/
function asyncFunc(work) {
return new Promise(function(resolve, reject) {
if (work === "")
reject(Error("Nothing"));
setTimeout(function() {
resolve(work);
}, 1000);
});
}
asyncFunc("Work 1") // Task 1 -- Initiating the promise
/* ---------------------------------------
Handling the result from the first promise
------------------------------------------*/
.then(function(result) {
console.log(result);
return asyncFunc("Work 2"); // Task 2 -- Initiating the promise again
}, function(error) {
console.log(error);
})
/* ---------------------------------------
Handling the result from the second promise
------------------------------------------*/
.then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
});
console.log("End");