-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-Promise.js
More file actions
46 lines (41 loc) · 1.54 KB
/
9-Promise.js
File metadata and controls
46 lines (41 loc) · 1.54 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
// We can see that this is much cleaner and easier to understand than the previous callback example.
let getData=function(message){
return new Promise(function(resolve,reject){
resolve("abhinav is saying "+message+"! You want more?");
});
}
let getMoreData=function(message){
return new Promise(function(resolve,reject){
resolve("Rahul is saying "+message+"! You want more??");
});
}
let getSomeMoreData=function(message){
return new Promise(function(resolve,reject){
resolve("Gagan is saying "+message+"! You want more??");
});
}
let get_SomeMore=function(message){
return new Promise(function(resolve,reject){
resolve("Nirmal is saying "+message+"! GOODBYE!!!");
});
}
getData("Hi").then(function(fromResolve1){
console.log(fromResolve1);
return getMoreData("Hello");
}).then(function(fromResolve2){
console.log(fromResolve2);
return getSomeMoreData("Kidaan");
}).then(function(fromResolve3){
console.log(fromResolve3);
return get_SomeMore("Chad di kala");
}).then(function(fromResolve4){
console.log(fromResolve4);
});
// It takes an array as an input.Promise.all() waits for all promises to succeed and fails if any
// of the promises in the array fails.
// All the promises run simultaneously .they don’t have to wait for the previous one to finish
// Promise.all([getData("Hi"),getMoreData("Hello"),
// getSomeMoreData("Kidaan"),get_SomeMore("Chad di kala")
// ]).then(function(messages){
// console.log(messages);
// });