1/*
2	A Promise is a proxy for a value not necessarily known when the promise is created. 
3    It allows you to associate handlers with an asynchronous action's eventual success 
4    value or failure reason.
5*/            
6let promise = new Promise((resolve , reject) => {
7  fetch("https://myAPI")
8    .then((res) => {
9      // successfully got data
10      resolve(res);
11    })
12    .catch((err) => {
13      // an error occured
14      reject(err);
15    });          
16});1new Promise( (res, rej) => {
2  setTimeout(() => res(1), 1000);
3}).then( (res) => {
4  console.log(res); // 1
5  return res*2
6}).then( (res) => {
7  console.log(res); // 2
8});
91var promise = new Promise(function(resolve, reject) {
2  // do some long running async thing…
3  
4  if (/* everything turned out fine */) {
5    resolve("Stuff worked!");
6  }
7  else {
8    reject(Error("It broke"));
9  }
10});
11
12//usage
13promise.then(
14  function(result) { /* handle a successful result */ },
15  function(error) { /* handle an error */ }
16);1//Promise to load a file...
2//use with openFile(url, fCallback);
3//fCallback(fileContents){ //do something with fileContents}
4const loadFile = url => {
5    return new Promise(function(resolve, reject) {
6        //Open a new XHR
7        var request = new XMLHttpRequest();
8        request.open('GET', url);
9        // When the request loads, check whether it was successful
10        request.onload = function() {
11            if (request.status === 200) {
12                // If successful, resolve the promise
13                resolve(request.response);
14            } else {
15                // Otherwise, reject the promise
16                reject(Error('An error occurred while loading image. error code:' + request.statusText));
17            }
18        };
19        request.send();
20    });
21};
22const openFile = (url, processor) => {
23    loadFile(url).then(function(result) {
24            processor(result);
25        },
26        function(err) {
27            console.log(err);
28        });
29};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));1firstRequest()
2  .then(function(response) {
3    return secondRequest(response);
4}).then(function(nextResponse) {  
5    return thirdRequest(nextResponse);
6}).then(function(finalResponse) {  
7    console.log('Final response: ' + finalResponse);
8}).catch(failureCallback);