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.