0

I am trying to return a variable which is set by a Promise's then() in a JS function.
The function is called externally by including the myScript.js file
For eg:

myScript.js

function myFunc(){ 
   var myFieldToReturn; 
   functionThatReturnsPromise().then(function(data){\n
       //The value of "data" is used to perform some operations and 
       //should be returned to the caller of myFunc()
            myFieldToReturn = data + "Hi"; 
   });
   return myFieldToReturn;
}

functionThatReturnsPromise() is a function defined external to this script and is not modifiable. Call to myFunc is also external and I have no control on the way my return value is used (So, I cannot send back a promise as my function's return value).
Any help on would be appreciated.

Thank You

Venkata Krishna
  • 1,768
  • 2
  • 14
  • 21
  • 1
    There is no way to do this. You will have to modify the signature of the function to either return a `Promise` (preferred) or accept a `callback` – Dan Aug 03 '17 at 14:11
  • 1
    Then you're pretty much SOL. `myFunc` is asynchronous because `functionThatReturnsPromise` is asynchronous, so the caller of `myFunc` must also be asynchronous. You cannot "synchronise" an asynchronous function. – deceze Aug 03 '17 at 14:12

0 Answers0