jaystack / jaydata

Notice: this library isn't maintained anymore
http://jaydata.org
GNU General Public License v2.0
352 stars 94 forks source link

Promises A+ compliance #157

Open marklagendijk opened 10 years ago

marklagendijk commented 10 years ago

JayData queries return promises. Eg:

myContext.People
    .toArray()
   .then(people){
        //do something with people
    }

However, currently the promises are not A+ compliant. The main issue is that the function passed to the then method is not called asynchronously when the promise is already fulfilled. This leads to situations where the same function is sometimes called synchronously and sometimes asynchronously.

The spec states that it should always be called asynchronously:

onFulfilled or onRejected must not be called until the execution context stack contains only platform code. Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such as setTimeout or setImmediate, or with a “micro-task” mechanism such as MutationObserver or process.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called.

I would really like to see this fixed.

marklagendijk commented 10 years ago

It looks like JayData uses either jQuery or q promises, based on whether these are loaded. jQuery promises are not A+ compliant, but q promises are. Does JayData favor q promises over jQuery promises?