0

In file A, I am getting a promise inside a IIFE, and updating a variable. This variable is returned(exposed for other files).

var ServiceResponse = (function() {
var cardPattern = null;
getCardPattern().then(function(response) {
    console.info('Successfully fetched card pattern details.');
    cardPattern = response;
    App.init();
},
function(error) {
    console.error('Failed fetching card pattern details!');
});
return {
    cardPattern : cardPattern
}
})();

In file B, I am trying to consume this returned variable but i am getting null and not the updated value from the promise response.

var validateCreditCard = function(cardNumber, expiryMonth, expiryYear, cvv) {
var creditCardPattern = ServiceResponse.cardPattern;

I am making sure that from file B invocation is done only after the promise is returned, but no luck. Why?

ramesh en
  • 93
  • 1
  • 2
  • 10
  • https://stackoverflow.com/questions/32494821/how-to-create-javascript-object-using-iife - but also async so several issues – mplungjan Oct 08 '17 at 13:55
  • There is code missing. What is `getCardPattern()`? If it is an asynchronous call, that might be a part of the results you are experiencing. The second code example is incomplete (no closing curly brace). – Alan Larimer Oct 08 '17 at 13:55
  • 2
    The `then` callback is async. There is no `cardPattern` until it completes. – adeneo Oct 08 '17 at 13:55
  • @AlanLarimer - getCardPattern is a function, inside which there is a ajax call made. basically it is returning a promise. no the result is not null. it is valid response. second code example is just a snippet, line 2 is important. – ramesh en Oct 08 '17 at 13:58
  • @adeneo - Could you pls elaborate? I am making sure that only after the promise is resolved, i am trying to fetch the value.(already mentioned in the question) – ramesh en Oct 08 '17 at 14:01
  • @mplungjan - That actually helped...perfect! – ramesh en Oct 08 '17 at 14:06
  • @adeneo so https://stackoverflow.com/questions/32494821/how-to-create-javascript-object-using-iife is lilely more relevant dupe – mplungjan Oct 08 '17 at 14:11

0 Answers0