0

I'm trying to return a promise inside another by nesting two resolve functions as follows:

const a = new Promise((resolve1, reject1) => { 
    resolve1(new Promise((resolve2, reject2) => { 
        setTimeout(() => resolve2('finished'), 6000) 
    }).catch((err) => reject1(1))); 
});

The result of the operations await a is finished instead of Promise {'finished', ...}

After that I also tried using chaining

const a = new Promise((resolve, reject) => resolve('finished'))
    .then((phrase) => { 
        return new Promise((resolve, reject) => resolve(phrase)) 
    });

Unfortunately, I got the same result as in the previous attempt. Why is it wrong to do it either way? What would be the correct way to do this without using extra class definitions?

Juan Lopez
  • 67
  • 1
  • 4
  • 2
    `await promise` will return the promise result and not the actual promise. If you want the actual promise you can do `const promise = p; await promise;` then `promise` will be a resolved promise – apokryfos Jul 10 '18 at 17:24
  • The code you posted isn't wrong, it follows the spec. – Adam Jenkins Jul 10 '18 at 17:25
  • What's the purpose of the nested resolves? – Batajus Jul 10 '18 at 17:25
  • @apokryfos yeah, but the result should be given by `await await promise` the way I want it to be – Juan Lopez Jul 10 '18 at 17:25
  • 4
    That sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – apokryfos Jul 10 '18 at 17:26
  • 1
    @JuanLopez - that's crazy. Why? – Adam Jenkins Jul 10 '18 at 17:26
  • 1
    @Batajus It is for an operation that takes a little bit of time to setup and then it starts doing a heavy operation, so I want the first promise to signal me when it has finished to set up and the inner promise to know when it has finished. – Juan Lopez Jul 10 '18 at 17:27
  • As it was said, this is XY problem. Please, provide all necessary information regarding your real problem instead of expected solution to this problem (which is wrong). – Estus Flask Jul 10 '18 at 17:28
  • @JuanLopez - you don't understand promises, the second promise will never start to execute until the first one is finished (and the result returned) anyway. – Adam Jenkins Jul 10 '18 at 17:29
  • @Adam Ok, that's what I was looking for. Thanks! But Idk why though. – Juan Lopez Jul 10 '18 at 17:30
  • I have already found another solution so I'll rephrase the question to just ask why this is not possible. – Juan Lopez Jul 10 '18 at 17:34
  • 2
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jul 10 '18 at 17:35
  • @Bergi thanks! that's what I was looking for. – Juan Lopez Jul 10 '18 at 17:44
  • Btw, the `.catch((err) => reject1(1))` doesn't really work - the callback would happen after the call to `resolve1` and not do anything. Even worse, this produces a promise that is *fulfilled with `undefined`* instead of being rejected, and that's the promise that your `resolve1(…)` with. – Bergi Jul 10 '18 at 17:47

0 Answers0