-
Notifications
You must be signed in to change notification settings - Fork 0
/
promises.js
75 lines (59 loc) · 1.74 KB
/
promises.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//blog posts variable
//an array of objects
const posts = [
{title: 'post one', body: 'this is post one'},
{title: 'post two', body: 'this is poat two'}
];
//suppose fetching data from a server
//which can take couple of seconds
//it is mimic by setTimeout() function
//in here arrow function syntax has been used
function getPosts(){
setTimeout(() => {
let output = '';
//iterate through posts collection
//in each iteration executes the arrow function
posts.forEach((post, index) =>{
output += `<li>${post.title}</li>`;
});
//adding output value to page
document.body.innerHTML = output;
}, 1000);
}
//with promises////////////////////////////
function createPosts(post){
//return a promise
//if ok ==> call resolve
//if not ok ==> call reject
return new Promise((resolve, reject) => {
setTimeout(() =>{
posts.push(post);
const error = false;
if(!error){
resolve();
}
else{
reject('Error: something went wrong');
}
},2000);
});
}
// ///if it resolved ==> then comes to .then part
// createPosts({title: 'post three', body:'this is post three'})
// .then(getPosts)
// .catch(e => console.log(e));
//for multiple promises see below code
//above code has commented out
//Promise.all
const promise1 = Promise.resolve('hello world');
const promise2 = 10;
const promise3 = new Promise((resolve, reject) =>{
setTimeout(resolve, 2000, 'good bye');
}
);
//for this promise creation, fetch API has been used
//need to parse into json objects
const promise4 = fetch('https://jsonplaceholder.typicode.com/users').then(response => response.json());
Promise.all([promise1, promise2, promise3, promise4]).then(values => {
console.log(values);
});