Description
This came out of a discussion with MarkM and @kriskowal. It seems promising.
Start from this formulation of the "promise constructor" idea we've seen in many other issues so far:
var promise = new Promise(function (resolve, reject) {
resolve(5);
});
Imagine if instead we specced a thenable assimilator, we'll call it when
. Then we could instead accomplish the above with:
var promise = when({ then: function (resolve, reject) {
resolve(5);
} });
This is between one and three more characters, depending on if you prefer {x:y}
or { x: y }
.
ES6 style:
let promise = new Promise((resolve, reject) => resolve(5));
let promise = when({ then(resolve, reject) => resolve(5) });
This time between -1 and +1 character deltas.
Q of course gets it easy, because our assimilator would be called Q
, making it a win on characters in every case.
Important note: then
must be executed in the next turn of the event loop to prevent malicious thenables from breaking execution flow. (On the plus side, this means that any calls to resolve
or reject
can synchronously inform the returned promise
, since we're already in the next tick.)
Alternately we could spec both assimilation and construction at the same time, given an equivalence between two. Maybe new Promise((resolve, reject) => { ... })
is equivalent to Promise.from({ then(resolve, reject) => { ... })
?