Promise Chaining

by Allie Yu

JavaScript

let wordnikAPI = "https://developer.wordnik.com/";
let giphyAPI = "https://developers.giphy.com/"

function setup(){
    //let promise = fetch(wordnikAPI);
  fetch(wordnikAPI).then(gotData).catch(gotErr);
  function gotData(data){
      console.log(data);
  }
  function gotErr(err){
      console.log(err);
  }
}

//=>
function setup2(){
    fetch(wordnikAPI)
      .then(function(data){
          console.log(data);
  }).catch(function(err){
          console.log(err);
  });
}

//=>
function setup3(){
    fetch(wordnikAPI)
      .then(response => console.log(response.json))
    .then(json => createP(json.word)
    .catch(err => console.log(err));
}
//fetch will return a json, need parse it


//=>
function setup3(){
    fetch(wordnikAPI)
      .then(response => {
        return response.json();
    })
    .then(json => { 
        createP(json,word);
      return fetch(giphyAPI + json.word);
    })
    .then(response => {
        return response.json();
    })
    .then(json => {
        createImg(json.data[0].image['fixed_height_small'].url)
    })
    .catch(err => console.log(err));
}



/*
var p1 = new Promise((resolve,reject) =>{
    fs.readFile('file.txt',(err,data) => {
  if (err) return reject(err)
  resolve(data);
  });
}).then(function(data){
    return 10
}).then(function(data){
    return p2();
}).then(function(data){
    return p3();
}).catch(function(err){
    //handle error
})
*/