You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make iteration query methods respect returned promises (#1112)
* Make ParseQuery.map respect returned promises
This function is documented as allowing the callback to return a
promise, and when the callback returns a promise we should wait for that
promise to resolve before continuing, and stop iteration if the promise
rejects.
If the callback returned a promise, the previous implementation would
return an array of promises, and run the callback on all of them. The
new implementation wraps the returned value in `Promise.resolve`,
coercing it into a promise, so that we can safely extract a value from
it before pushing the value into the results array. Returning this
promise in the `.each()` callback also provides the behavior of waiting
/ stopping based on the resolution of a promise from the callback.
* Make ParseQuery.reduce respect returned promises
This function is documented as allowing the callback to return a
promise, and when the callback returns a promise we should wait for that
promise to resolve before continuing, and stop iteration if the promise
rejects.
The previous implementation was a bit naive in that it would load the
entire set of objects matching the query into memory, and then call
`.reduce()` on it. The new implementation avoids this by applying the
callback on each object in turn. This lets us then return the result of
the callback to the `.each()` call, which handles waiting for promise
resolution / rejection.
Things here get a little tricky in that the behavior of `reduce` is
special when no initial value is provided: we take the first item, don't
pass it into the callback, and treat that item as the initial value. We
need to keep that treatment, which entails a check for a special case:
we're looking at the first item AND the initial value was undefined.
* Make ParseQuery.filter respect returned promises
This function is documented as allowing the callback to return a
promise, and when the callback returns a promise we should wait for that
promise to resolve before continuing, and stop iteration if the promise
rejects.
When the callback returns a promise, the old behavior would have been to
push the object into the results array, because a promise is a truthy
value. Now we wait for the promise to resolve before checking the value.
* Reject when reducing empty result set with no initial value
This more closely mirrors the behavior of Array.reduce(), which throws a
TypeError in this situation.
Implementation notes:
- We return a rejected promise instead of throwing synchronously,
because we need to wait for the result set to materialize before we
know if you're reducing on an empty set
- The `.each()` callback is never invoked for an empty result set, so we
need to have this empty check happen _after_ we've finished iteration.
Fortunately we're already keeping track of the iteration index in this
scope, so we can reuse that for this check.
Thanks for the feedback @acinader!
0 commit comments