1A promise is an object that may produce a single value sometime in the future: either a resolved value, or a reason that it's not resolved (e.g., a network error occurred)
1Promises make async javascript easier as they are easy to use and write than callbacks. Basically , promise is just an object , that gives us either success of async opertion or failue of async operations
1getData()
2 .then(data => console.log(data))
3 .catch(error => console.log(error));
1var posts = [
2 {name:"Mark42",message:"Nice to meet you"},
3 {name:"Veronica",message:"I'm everywhere"}
4];
5
6function Create_Post(){
7 setTimeout(() => {
8 posts.forEach((item) => {
9 console.log(`${item.name} --- ${item.message}`);
10 });
11 },1000);
12}
13
14function New_Post(add_new_data){
15 return new Promise((resolve, reject) => {
16 setTimeout(() => {
17 posts.push(add_new_data);
18 var error = false;
19 if(error){
20 reject("Something wrong in </>, Try setting me TRUE and check in console");
21 }
22 else{
23 resolve();
24 }
25 },2000);
26 })
27}
28
29New_Post({name:"War Machine",message:"I'm here to protect"})
30 .then(Create_Post)
31 .catch(err => console.log(err));