voger
voger

Reputation: 676

What is qx.event.type.Event.addPromise() used for?

I was looking through the apiviewer and I noticed there is a method addPromise(promise) in the class qx.event.type.Event. I searched through the qooxdoo organization in Github for use cases but I couldn't find any. Can someone provide an example of use for that method?

Upvotes: 1

Views: 154

Answers (1)

johnspackman
johnspackman

Reputation: 1003

It's because collecting promises typically relies on a function returning the promise, but the event firing API is baked in with the return value being a boolean (see fireEvent and fireDataEvent in MEvent.js.

In other circumstances, we've added a new function with the Async suffix (eg property called "myProperty" can have setMyPropertyAsync) which allows us to separate a promise based path through the code from the old non-promise, synchronous code - so we're able to introduce new features while maintaining strict backwards compatibility (features which would otherwise require a breaking API change)

But that's not possible with events, because fireEvent/fireDataEvent are so ubiquitous and fundamentally synchronous, so they must continue to return a boolean.

We're trading on the fact that events are typically used in an asynchronous way, and the fact that the boolean return value of fireEvent is only used to determine whether the event was preventDefault()-ed or not. This makes it a requirement that events cannot be preventDefaulted() asynchronously (well, they probably can in reality but there could conceivably be a situation where it is not 100% reliable to do so).

However, because the return value is not a promise and cannot be chained, we have to provide a mechanism to chain it - the addPromise method allows callers to add promises to the chain, and then the framework will grab those promises later by calling event.promise() - see qx.event.Registration.fireNonBubblingEventAsync.

Upvotes: 3

Related Questions