JavaScript Angular $q library to chain methods after success of an $http request to api?

I've never written a promise before but I feel like this code communicates my intent.

Question: How can I asynchronously fire off matchData() and countData() in the init block, after my http request has been completed, transformed and returned.

 function getLangData(langCode) {

    var url = "https://translate.google.com/translate_a/l?cb=JSON_CALLBACK";
      return $q(function(resolve, reject) {    
      $http.jsonp(url, {
        params: {...}
      })
        .success(function (translateAPIData) {
                 return translateAPIData.map(function(){...});
          });
        });

      });
  }

function init() {
  var promise = getLangData(languageCode);
  promise.then(function(mappedData) {
     matchData(mappedData);
  });
  promise.then(function(mappedData) {
     countData(mappedData);
  });  

  }); 
}
Answer:1

You don't need to create you own promise using $q as $http method does return a promise by default, you could utilize that by calling .then method on it.

function getLangData(langCode) {

    var url = "https://translate.google.com/translate_a/l?cb=JSON_CALLBACK";
      //returned promise object from method
      return $http.jsonp(url, {
        params: {...}
      })
      .then(function (response) {
           var translateAPIData = response.data;
           return translateAPIData.map(function(){...});
      });
}

Code

You could call the promise method just by doing .then on that method.

function init() {
  var promise = getLangData(languageCode);
  promise.then(function(mappedData) {
     matchData(mappedData);
     countData(mappedData);
  }); 
};
Answer:2

I cannot make the ratings element displayed. It simply does not appear. All other things work fine enough I have linked these properly: Semantic UI CSS (http://semantic-ui.com/dist/semantic.min.css) ...

I cannot make the ratings element displayed. It simply does not appear. All other things work fine enough I have linked these properly: Semantic UI CSS (http://semantic-ui.com/dist/semantic.min.css) ...

I want to create a search bar for my website where it can detect input from the user in the form of typing. How do I start going about this such that when the user starts typing he/she immediately ...

I want to create a search bar for my website where it can detect input from the user in the form of typing. How do I start going about this such that when the user starts typing he/she immediately ...

My goal: If the Camera button on my site is clicked > open the smartphone Camera > take a picture from a qr-code > resize it with js > load the picture on my server > read the qr-code > give me the ...

My goal: If the Camera button on my site is clicked > open the smartphone Camera > take a picture from a qr-code > resize it with js > load the picture on my server > read the qr-code > give me the ...

I'm looking how to zip an array of objects with values including a new key for each value using lodash. Tried with zip, zipObject and map but I don't find the key. What I want to do is the following (...

I'm looking how to zip an array of objects with values including a new key for each value using lodash. Tried with zip, zipObject and map but I don't find the key. What I want to do is the following (...

  1. lodash array object
  2. lodash array object contains
  3. lodash array object merge
  4. lodash object array sort
  5. lodash object array filter
  6. lodash object array difference
  7. lodash object array to string
  8. lodash object array unique