Writing a promise for the first time may seem daunting, but this code effectively conveys my intention.
We want to know:
How can we ensure that matchData()
and countData()
are asynchronously triggered in the init block after the completion of the http request, transformation, and return of data?
function fetchLanguageData(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 = fetchLanguageData(languageCode);
promise.then(function(mappedData) {
matchData(mappedData);
});
promise.then(function(mappedData) {
countData(mappedData);
});
});
}