promises-aplus / promises-spec

An open standard for sound, interoperable JavaScript promises—by implementers, for implementers.
https://promisesaplus.com/
Creative Commons Zero v1.0 Universal
1.84k stars 164 forks source link

Questions about implementation #213

Closed alexbyk closed 9 years ago

alexbyk commented 9 years ago

Hi. I'm trying to implement Promises/A+ (not in JS) and need help. (I need to decide could these paragraph be ignored for the flexibility )

1) Can someone point me what was the very first reason of this:

onFulfilled or onRejected must not be called until the execution context stack contains only platform code.

For example: avoiding deep recursion/stack overflow/etc. What else?

2) and this:

If onFulfilled is not a function, it must be ignored.
If onRejected is not a function, it must be ignored.

Why not 'null values must be ignored'? Because in my case onFulfilled can be not a function, but callable objects (overloaded instance). Also ignoring anything doesn't feel right for me

Thanks!

bergus commented 9 years ago

Can someone point me what was the very first reason of this:

onFulfilled or onRejected must not be called until the execution context stack contains only platform code.

Have a look at #104, #139 and all the ones linked there. This requirement is a crude formulation of the goal "_the callback must _always* be called asynchronously*" without specifying exactly how. The primary goal is that a callback passed to .then() is guaranteed not to be executed immediately right from within the then() (convenience), the secondary goal is that no client of a promise can influence timing of callbacks to his advantage (security). What this means in other languages, especially ones that don't use an event loop but have different concurrency models, is left to be determined by you.

Why not 'null values must be ignored'? Because in my case onFulfilled can be not a function, but callable objects (overloaded instance).

In JavaScript, a function (typeof x == "function") is exactly that: a callable object. The spec basically says: if the object is callable, use it, if not, ignore it. In other languages, especially ones with static typing, it makes sense to only allow functions as parameters in the first place - we just could not do this in JS. In your case, accepting "callable objects" (to be used) and null values (to be ignored) and nothing else is totally sensible.

alexbyk commented 9 years ago

@bergus thanks. this https://github.com/promises-aplus/promises-spec/issues/139 is the explanation for 1) I was looking for.