-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strawman: Promise Creation API/B #13
Comments
This strawman is based on a few guiding ideas:
|
I don't like Notes-1: Consider this function: function doAsyncWork() {
return new Promise(function (resolver) {
var resA = doSynchronousPreparationWhichMightThrowAnException();
resolver.resolve(doAsyncWork(resA));
});
} If you want to call it and be sure of handling all errors you need code to handle both synchronous and asynchronous errors, this is really bad. Otherwise this is mostly fine. I'd be inclined to cut |
I strongly oppose points 3 and 4. This would prevent any inheritance approaches (and this constructor proposal is all about inheritance, isn't it?). While I really like For example, in my https://github.com/bergus/F/blob/master/Promise.js I have implemented a My proposal would be to omit point 3 (2 should be enough), and change 4 to:
PS: Didn't we forget something substantial (point 0 or so):
|
I think points 3 and 4 only refer to a promise created from |
OK, that's good (though it excludes implementations that want to return subclass instances from the Still they don't seem really necessary to me, it would be nice to add at least some reasoning. Also, point 3 should be fixed to
|
|
Terminology
The Promise Constructor
new
, with the same results.promise instanceof Promise
must be true.Object.getPrototypeOf(promise) === Promise.prototype
must be true.promise.constructor === Promise.prototype.constructor === Promise
must be true.factory
is not a function, the implementation must throw aTypeError
.factory
is a function, it must be called immediately (i.e. within the same turn of the event loop), with a resolver. (note 1)The Resolver
The resolver is a function with several properties, which are also functions.
this
value.resolver(x)
x
is a non-promise,promise
is pending,promise
must be fulfilled withx
as its fulfillment value.promise
is settled, nothing happens (in particular, no exception may be thrown).x
is a promise,promise
must assume the state ofx
(see Promises/A+ spec).resolver.fulfill(value)
value
is a non-promise,promise
is pending,promise
must be fulfilled withvalue
as its fulfillment value.promise
is settled, nothing happens (in particular, no exception may be thrown).value
is a promise, the implementation must throw aTypeError
.resolver.reject(reason)
reason
is a non-promise,promise
is pending,promise
must be rejected withreason
as its rejection reason.promise
is settled, nothing happens (in particular, no exception may be thrown).reason
is a non-promise, the implementation must throw aTypeError
.resolver.yield(otherPromise) [normative optional? cut entirely?]
otherPromise
is a promise,promise
is pending,promise
must assume the state ofotherPromise
(see Promises/A+ spec).promise
is settled, nothing happens (in particular, no exception may be thrown).otherPromise
is not a promise, the implementation must throw aTypeError
.Notes
factory
throws an exception, since it is called within the same turn of the event loop asnew Promise
,promise
will never be successfully created and the error will be uncaught by the implementation. [is this note even necessary?]The text was updated successfully, but these errors were encountered: