0

I believe this question is about variable scoping and promise.

The first alert works as expected. However, I am getting undefined for the second alert. I was expecting [object] [Object] for the second alert just like the first alert.

Do you know why I am getting "undefined" for the second alert? What did I do wrong? Please help.

var result;

function isAvailable() {
  return Promise.resolve(true);
}

function getOrdersManagement() {
  return new Promise(function(resolve) {
    var powerOrdersMPageUtils = {
      id: "Hello",
      value: "World"
    }

    isAvailable().then(function(isAvailable) {
      if (isAvailable) {
        resolve(powerOrdersMPageUtils);
      }
    });
  });
}

function hasMethodOnPowerOrdersHost() {
  return Promise.resolve(true);
}

function getRouteJSONBySynonymIds() {
  var object = {
    name: "getRouteJSONBySynonymIds"
  }
  return Promise.resolve(object);
}

function getRouteJSON() {
  var object = {
    name: "getRouteJSON"
  }
  return Promise.resolve(object);
}



var result;

getOrdersManagement().then(
  function(ordersManagement) {
    hasMethodOnPowerOrdersHost().then(
      function(methodExists) {
        if (methodExists) {
          getRouteJSONBySynonymIds().then(
            function(object) {
              result = object;
              alert(result);
            }
          );
        }
        else {
          getRouteJSON().then(
            function(object) {
              result = object;
            }
          );
        }
      });
    });

alert(result);
Hello
  • 1

0 Answers0