On Wed, Nov 26, 2014 at 1:59 AM, James Burke <
jrb...@gmail.com> wrote:
> On Tue, Nov 25, 2014 at 6:38 AM, Tim Chien <
timd...@mozilla.com> wrote:
>> On Tue, Nov 25, 2014 at 10:09 PM, Boris Zbarsky <
bzba...@mit.edu> wrote:
>> I would imagine a Promise interface that runs synchronously can be
>> useful in many use cases other than this (for example, wrapping
>> IndexedDB IDBRequests in web content), but I do acknowledge we should
>> stop calling such interface "Promise".
>>
>> To speak specifically about DOMRequest#then, by looking at the patch
>> in bug 839838, I think what I need is achievable without out touching
>> Promise.jsm; we can simply keep the callbacks passed to
>> DOMRequest#then ourselves and call them directly, instead of chain
>> them to the internal promise.
>
> Some more clarification on the above:
>
> * If DOMRequest manages the functions itself, it should still use an
> async trigger for calling the callbacks, to maintain Promise
> expectations.
>
I didn't know "Promise expectations" means the then() callbacks must
be called async. But even so we can still keep the method confirm to
Promise expectations by doing the following:
1. If the then() is called before the DOMRequest resolves/rejects,
call the onFulfill/onReject callbacks at the same loop of
onsuccess/onerror events.
2. If the then() is called after the DOMRequest resolves/rejects, call
the onFulfill/onReject callbacks asynchronously.
Both 1 and 2 still call the callback asynchronously and make the lock
survive long enough for (1)*, but not (2).
* (1) is only useful for simple use case like mentioned in the thread,
as soon as people do chain callbacks with normal promises like
Promise.resolve(req).catch(callback) or Promise.all([req,
req2]).then(callback) the callbacks will not get a open lock.
> * If the .then on the DOMRequest is called later, after initial
> completion of the underlying request, then if there are resources or
> locks needed it would need to re-acquire them. Not sure that is
> possible given the original post’s code where the lock is acquired
> outside the object that has the then() capability.
>
Exactly.
> In general, I think your suggestion of ‘“stop calling such an
> interface “Promise”' is the best choice.
>
> In this case, it just sounds like DOMRequest is not a good fit for
> promise use, based on the APIs that use them and their assumptions on
> releasing resources? If so, it is best to just remove the .then
> capability from DOMRequest, and document that this is why DOMRequest
> exists as a separate construct to promises.
>
Since DOMRequest is being used beyond IDB-related stuff, so then()
will still be useful even if it can't fulfill the use case in this
thread. We could just accept that as a defect without removing it.
> That, or the mozSettings API might need a different API approach. Does
> not help the IDB situation though.
>
> James