-
-
Save damanm24/117569d79467be5e832dacd012027e59 to your computer and use it in GitHub Desktop.
Promise Inception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let promiseURL = function(searchTerm) { | |
return new Promise(function(resolve, reject) { | |
$http.get('https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=' + searchTerm + '&count=1&offset=0&mkt=en-us&safeSearch=Strict', { | |
headers: { | |
'Ocp-Apim-Subscription-Key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' | |
} | |
}).success(function(response) { | |
resolve(response); | |
}).error(function(err, status) { | |
reject(err); | |
}) | |
}) | |
}; | |
let fetchImageURL = function() { | |
return new Promise(function(resolve, reject) { | |
var URLs = []; | |
for (var i = 0; i < $scope.poll.options.length; i++) { | |
promiseURL($scope.poll.options[i].text) | |
.then(function(data) { | |
console.log(data); //THIS CONSOLE.LOG OCCURS AFTER THE RESOLVE IS CALLED | |
URLs.push(data.value[0].contentURL); | |
}) | |
.catch(function(error) { | |
console.log(error); | |
}); | |
} | |
if(URLs.length == $scope.poll.options.length) { | |
resolve(URLs); | |
} else { | |
reject("Error URLs was blank"); //RETURNS A BLANK ARRAY | |
} | |
}) | |
} | |
$scope.submitChoice = function() { | |
var isValid = true; | |
if (isValid) { | |
fetchImageURL().then(function(data) { | |
console.log(data); | |
}); | |
} else { | |
console.log("Not Valid Poll"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment