Normalize values, thenables, and functions into a native Promise
A plugin system lets plugins return their result in any form: a plain value, a native promise, a promise from an old library (a thenable), or a function that computes the result and may throw. The host wants exactly one shape to work with: a native Promise.
Implement toPromise(input), which normalizes any input to a native promise.
toPromise(input) // => Promise
input is a native promise (input instanceof Promise and input.constructor === Promise), return that same promise.class Tracked extends Promise {}): return a new native promise that settles the same way.input() synchronously, with no arguments, before toPromise returns.
toPromise itself never throws.then method. The returned promise adopts its outcome:
then is called asynchronously (not during the toPromise call) with two callbacks: fulfill and reject.then after it settled, are ignored.then throws before calling either callback, reject with that error.then property throws, reject with that error.null, undefined, plain objects) becomes the fulfilled value.Promise.resolve, Promise.reject, or Promise.try.await toPromise(42); // => 42
const request = fetchUser(7);
toPromise(request) === request; // => true
await toPromise({ then: (ok) => ok('legacy') }); // => 'legacy'
await toPromise(() => 'computed'); // => 'computed'
toPromise(() => { throw new Error('bad config'); });
// => a promise rejected with Error('bad config'); nothing is thrown