0

I am trying to run a function that has Promises inside of and at the end, return a value. When I try logging foo, it comes back as undefined. I don't want to return the Promise but just the value I am returning at the bottom of the chain.

let foo = function test() {
     new Promise((resolve) => {
          resolve();
     })
     .then(() => {
         return "123"
     })
}

foo // undefined
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
RicardoAlvveroa
  • 226
  • 1
  • 8
  • 1
    Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. – Dave Newton Jan 07 '21 at 18:01
  • You can't, that's not how promises work. They are asynchronous and run in the background. To get the value, you'll need to use a `.then()` method and do whatever you want with the value as a callback. – gen_Eric Jan 07 '21 at 18:01
  • 1
    "*I don't want to return the Promise*" - you have to. "*just the value I am returning at the bottom of the chain.*" - that has not happened yet when `foo()` returns. – Bergi Jan 07 '21 at 18:03
  • but if I `return` the Promise, my return is then `Promise {: "123"}` – RicardoAlvveroa Jan 07 '21 at 18:27
  • If the return value of `foo` does not depend on the promise chain you can just move `return "123"` outside of the `then` callback, into the main `test` body. If it does depend on the promise chain the return value must also be a promise, since it eventually returns a value (once the promises it depend on are fulfilled). If the promise chain is always immediately fulfilled, there should not be a promise chain at all. In your example you could use `function test() { return "123" }`, since there is no need for promises. – 3limin4t0r Jan 07 '21 at 18:38
  • @3limin4t0r it does depend on the promise chain but if I add `return` before `new Promise` so that the Promise is being returned, I now get `Promise {: "123"}` as a result – RicardoAlvveroa Jan 07 '21 at 18:41
  • @RicardoAlvveroa If you don't want the promise use `return "123"` directly within `test` (not within a `then` callback). If it depends on data that is delivered through a promise, the return value of `test` must be a promise, because you cannot return the final result before the promise data resolves. – 3limin4t0r Jan 07 '21 at 18:43
  • what I'm doing there is dependent on the the prior stuff being run so it needs to be in the Promise chain. At the very end, I want to return a value. Putting it outside the Promise chain would execute it before the Promise has completed. – RicardoAlvveroa Jan 07 '21 at 18:46
  • @RicardoAlvveroa If it depends on the promise chain you cannot return a non-promise value. Say you run a store and I want to buy item x. When I run `buyItem("x")` you cannot give me the item directly. You must first check if the item is in stock. Me being JavaScript cannot wait for this check and want something from you this instant. So you promise me that you will give me item x if it is in stock. You cannot give me item x without checking the availability, so the best you can do is promise me something. – 3limin4t0r Jan 07 '21 at 18:55

0 Answers0