0

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);
});
Chaipau
  • 199
  • 1
  • 5
  • 14
  • 1
    Your code does not call `resolve()` in the Promise handlers. – Pointy Mar 19 '18 at 14:10
  • Where's `resolve(data)`? - Please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ele Mar 19 '18 at 14:11
  • 1
    Although in your case, `$.getJSON` already returns a promise – Benjamin Gruenbaum Mar 19 '18 at 14:11
  • 1
    When doing front end, you have to realize that there is no `built in javascript`. only `browser implementation`. For instance, Promise is not implemented in all browsers. you can check the browser compatibility table [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) (guess who's missing ?) – Logar Mar 19 '18 at 14:12
  • read more about Promises here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise It'll help you to understand what is going on, and prevent more errors in future – Andrew Evt Mar 19 '18 at 14:12
  • Please don't edit the tile when you get an answer. – Heretic Monkey Mar 19 '18 at 14:39

0 Answers0