0

Is it possible to access the value of result2 outside the promise. If yes how do I do it

Example

someFile1.someFunction1(req).then((result1)=>{
.
.
.
.

    someFile2.someFunction2(req).then((result2)={
        return(result2);
    });

    return(result1+result2);

})
georgeawg
  • 48,608
  • 13
  • 72
  • 95
David
  • 15
  • 7
  • 2
    I'm afraid the code you've shown is too fragmentary for us to understand how your real code is set up. Please update the example to show your real situation (but still minimal, see [this topic](/help/mcve) in the help for more). – T.J. Crowder Nov 14 '18 at 16:40
  • 1
    You can use Promise.all to achieve this result. Promise.all waits until all promises are resolved, and will then return the result of all the resolved promises. – enf0rcer Nov 14 '18 at 16:45
  • @enf0rcer - Maybe. I assumed they were nested for a reason. If they aren't, though, yeah, that would be the way to go. – T.J. Crowder Nov 14 '18 at 16:48
  • I wrote a function on a post similar to `Promise.all` that runs the promises sequentially. Give it a try. https://stackoverflow.com/questions/53275216/how-to-make-a-foreach-loop-wait-for-each-ajax-function-to-finish/53291163#53291163 – darklightcode Nov 14 '18 at 16:51

3 Answers3

4

Note: I'm assuming they're nested for a reason. Otherwise, use Promise.all to run them in parallel and add up the array you receive when it resolves.

If I assume your code really is largely as shown, that the .s don't introduce too much complexity, there are a couple of ways, but probably the simplest in that example is to just nest the promise handlers:

someFile1.someFunction1(req)
.then((result1) => {
    return someFile2.someFunction2(req)
        .then((result2) => {
            return result1 + result2;
        });
})
.then(combined => {
    // Use the combined result
});

or with concise arrow functions:

someFile1.someFunction1(req)
.then(result1 =>
    someFile2.someFunction2(req).then(result2 => result1 + result2)
)
.then(combined => {
    // Use the combined result
});

or of course, use an async function and await:

const result1 = await someFile1.someFunction1(req);
const result2 = await someFile2.someFunction2(req);
const combined = result1 + result2;

or even

const combined = (await someFile1.someFunction1(req)) + (await someFile2.someFunction2(req));
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

No it is not possible since at the point of the return statement, the Promise may not yet be resolved since that is asynchronous. What you can do is return another Promise that deals with the results of the other two by leveraging Promise.all. This solution only works if promise2 does NOT need to be nested within promise1. If it does need to be nested, look at the solution from T.J. Crowder.

function combinePromises() {
  var promise1 = Promise.resolve(3);
  var promise2 = Promise.resolve(42);

  var promise3 = Promise.all([promise1, promise2]).then(([result1, result2]) => result1 + result2);
  return promise3;
}
var resultPromise = combinePromises();
resultPromise.then(finalResult => console.log(finalResult));
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
-1

You can always pass an async function as a Promise callback:

const doSomething = function(someData) = {
  return new Promise(async (resolve, reject) => {
    try {
      const result1 = await somePromise(someData);
      const result2 = await someOtherPromise(result1);

      resolve(result1 + result2);
    } catch (ex) {
      reject(ex);
    }
  });
};
Mehmet Baker
  • 1,055
  • 9
  • 23
  • 2
    `new Promise(async ...` is an anti-pattern. `async` functions create promises, so it just creates an extra promise layer and adds that awkward `try`/`catch`. The simpler version of the above is: `const doSomething = (async() => (await somePromise('someData')) + (await someOtherPromise(result1, 'someOtherData')))();` Does the same thing (including error handling), but without the extra layer and awkward structure. – T.J. Crowder Nov 14 '18 at 17:01
  • 1
    @T.J.Crowder, oh, yes you are right. I was feeling something was off but haven't realized until you mentioned. Thanks – Mehmet Baker Nov 14 '18 at 17:05