0

Hi I'm having a question related to java script Promise.

var value;
Promise.resolve(await this.triggerAPI_MDBREADMI_GetFGLINHU2()).then(res => {
  if (a === 1) {
    RequestSearchController.runNumber = res;
  } else {
    RequestSearchController.runNumber = RequestSearchController.runNumber + 1;
  }

  value = RequestSearchController.runNumber;
});

console.log(value);

I need to assign the RequestSearchController.runNumber to an external variable, 'value'. But my results are undefined. can someone help me to identify the issue that I have made

Taki
  • 17,320
  • 4
  • 26
  • 47
t4thilina
  • 2,097
  • 3
  • 18
  • 19
  • offtopic, why are you not checking `a === 1` before doing the call to `triggerAPI_MDBREADMI_GetFGLINHU2`? – Lawrence Cherone Sep 23 '19 at 20:21
  • 1
    Right, so the answer for why value is undefined is that, actually, value is defined. It's just being logged before a value is assigned to it. the promise is asynchronous, meaning the `console.log` will be triggered before the code in the promise is executed, resulting in show it's `undefined` value. Value **is** defined after the promise resolves, you just logged it before it became that. Put the `console.log` IN the promise callback at the bottom and you'll log it after it's been defined. – Jacob Penney Sep 23 '19 at 20:25

2 Answers2

1

Value is defined.

It's just being logged before a value is assigned to it. the promise is asynchronous, meaning the console.log will be triggered before the code in the promise is executed, resulting in show it's undefined value. Value is defined after the promise resolves, you just logged it before it became that. Put the console.log IN the promise callback at the bottom and you'll log it after it's been defined.

Anitpatterns

When you're using async functions, the await keyword pauses the function from executing until the promise after the await resolves. Therefor, you don't need to wrap it in a Promise.resolve or use the .then() pattern.

Fixed code:

var value;
var res = await this.triggerAPI_MDBREADMI_GetFGLINHU2())

RequestSearchController.runNumber = (a === 1 ? res : RequestSearchController.runNumber +1);

value = RequestSearchController.runNumber;

console.log(value);
Jacob Penney
  • 725
  • 4
  • 9
0

Is RequestSearchController undefined? If so that would be a separate API problem.

If you are getting a valid response, simply put the console.log inside of a .then. Also you dont need await

var value;
Promise.resolve(this.triggerAPI_MDBREADMI_GetFGLINHU2()).then(res => {
  if (a === 1) {
    RequestSearchController.runNumber = res;
  } else {
    RequestSearchController.runNumber = RequestSearchController.runNumber + 1;
  }

  value = RequestSearchController.runNumber;
})
// New code here v
.then( () => console.log("value", value))

inside a for loop/ return value from a function:

var value;
myFunc = () => {
for (var i = 0; i < something; i++) {
Promise.resolve(this.triggerAPI_MDBREADMI_GetFGLINHU2()).then(res => {
  if (a === 1) {
    RequestSearchController.runNumber = res;
    return RequestSearchController.runNumber;
  } else {
    RequestSearchController.runNumber = RequestSearchController.runNumber + 1;
    return RequestSearchController.runNumber + 1
  }
})
// New code here v
.then( res => value = res)
}
console.log(value) // value only prints once
return value
}
Cory Baker
  • 167
  • 1
  • 13