0

I have the following code:

const a = {
    Top: {
        fun: () => {
            return {
                sub: {
                    variable: getResponse().then((response) => response)
                }
            };
        }
    }
};

getResponse() returns a Promise.resolve().

I then try to get the value of variable in another file by doing the following:

let test = importedFile.a.Top.fun().sub.variable.then((resp) => resp);

But when I print out the value of test, instead of getting the value of variable, I get

Promise { <pending>, domain: Domain { domain: null, _events: { error: [Function: handleException] }, _eventsCount: 1, _maxListeners: undefined, members: [] } }

I am sure that in the actual const, variable is getting set to the correct value. I'm just unsure of how to access this separately.

My question is unique as the solution to the other question does not solve this problem.

  • `((response) => resp)`? What’s `resp`? The result of calling `then` will still be a Promise. `then((x) => x)` doesn’t do anything useful. You need to use `response` in your callback. – Sebastian Simon Aug 01 '18 at 18:22
  • @mpm And then? `test` isn’t used in that `then` callback of yours. – Sebastian Simon Aug 01 '18 at 18:22
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Roamer-1888 Aug 01 '18 at 18:24
  • 1
    Possible duplicate of [Fetch returns promise instead of actual data even after using 'then'](https://stackoverflow.com/questions/39021870/fetch-returns-promise-instead-of-actual-data-even-after-using-then), or any question when you simply Google “js then returns promise instead of value”. You could also read the [docs on `then`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then). – Sebastian Simon Aug 01 '18 at 18:25
  • user8895105, listen to what you are being told. All the comments and both answers are saying the same thing. Your question is far from unique. Many people encountering Promises for the first time experience the same issue. Once you have managed to get your mind round Promises, they will be your best friends. – Roamer-1888 Aug 01 '18 at 19:05
  • [Drop the pointless `.then(value => value)`](https://stackoverflow.com/q/41089122/1048572) – Bergi Aug 01 '18 at 19:20

2 Answers2

1

Once a value is encapsulated in a Promise, it is not possible to get it back out. This is by design. You are forced to write code that uses the encapsulated value in the context of a .then block. This prevents you from using the value before the asynchronous code has resolved it's value.

let test = importedFile.a.Top.fun().sub.variable.then((resp) => resp);
expect(test).to.equal(resp); 

This does not work because test is a Promise<typeof resp>, not typeof resp

You would need to do something like this

importedFile.a.Top.fun().sub.variable.then((resp) => resp)
  .then(value => {
    expect(value).to.equal(resp);
  });
Robert Stiffler
  • 675
  • 3
  • 10
-1

That is because you are assigning a promise to your variable. If your getResponse() function returns a promise, chaining with then or catch is still going to return a promise so you can continue to chain functions to your promise. What you are looking for is something like this:

importedFile.a.Top.fun().sub.variable.then(response => {
    // Continue executing code based on the value of this variable...
    // Where 'response' is equal to what you intended 'test' to be from your original post
});
David Meza
  • 3,080
  • 3
  • 16
  • 18
  • Assigning the resolved value to a variable outside of the context of the Promise is usually a bad idea. The Promise executes asynchronously, so there is no guarantee when the value of `test` will be assigned. If `test` is used on the line immediately following your example, it may or may not be `undefined`. – Robert Stiffler Aug 01 '18 at 18:33