I am not able to implement Promise in javascript.
Here is the snippet of the js file where I want to implement Promise.
var promiseToGetUsage = new Promise(function(resolve) {
setTimeout(function() {
$.getJSON('../usageService/resources/webservice/getUsageDetails',
function(data) {
console.log(data);
});
}, 1000);
});
var promiseToGetCompute = new Promise(function(resolve) {
setTimeout(function() {
$.getJSON('../usageService/resources/webservice/getComputeDetails', function(data) {
console.log(data);
});
}, 1000);
});
Promise.all([promiseToGetUsage, promiseToGetCompute]).then(function(value) {
self.isLoading(false);
self.isUsageVisible(true);
self.isComputeVisible(true);
});
Here I don't understand two things.
- i) The netbeans checker says "The global variable "Promise" is not declared", while I thought it is inbuilt js function
ii) I did some research on (i) and added the below in script tag in HTML
src="//cdn.jsdelivr.net/bluebird/3.5.0/bluebird.js"
Now when I debug, it goes to the Promise.all first, but when the webservices are completed, it does not call the Promise.all again!
**********************************ANSWER****************************************
Thanks to Benjamin Gruenbaum for pointing in the right direction in the comments,
Solution, is to directly call the $.getJson in Promise's all method
Promise.all([
$.getJSON('../usageService/resources/webservice/getUsageDetails',
function (data)
{
console.log(data);
}),
$.getJSON('../usageService/resources/webservice/getComputeDetails',
function (data)
{
console.log(data);
})
]).then(function(value) {
self.isLoading(false);
self.isUsageVisible(true);
self.isComputeVisible(true);
});