0

Suppose that x is a Promise. Then Promise.resolve(x) is exactly the same thing as x. However, I would like to create a Promise that returns a Promise, i.e., some function magic such that magic(x) is a fullfilled Promise whose value is x.

Using TypeScript annotation for better clarity:

let x : Promise<number> = whatever();
let y : Promise<number> = Promise.resolve(x);
let z : Promise<Promise<number>> = magic(x);
Console.assert(x === y);
z.then(function (a) {
  Console.assert(x === a);
});

How can magic be implemented?

The same question can be asked for the Promise.then method, but I guess that the answer would be pretty similar.

Giovanni Mascellani
  • 1,218
  • 2
  • 11
  • 26
  • You would need to use an algebraic promise library such as [Creed](http://blog.briancavalier.com/creed/) where you can use `Promise.of(Promise.of(someValue))`, the `then` and `resolve` methods of A+ compatible libraries can never do this – Bergi Dec 11 '17 at 13:22
  • For the record, inspired by answers to https://stackoverflow.com/q/32168194/807307, I ended up defining `z` as `Promise<[Promise]>` and packing and unpacking the inner promise each time I had to. It is a bit ugly, but the array around the inner promise prevents the outer promise to resolve to it. It seems to work. – Giovanni Mascellani Jan 31 '18 at 10:53

0 Answers0