[WIP] Promisify client-facing methods #225
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change makes client-facing methods return a
Promise
when nocallback
is provided.Motivation
Promise
s andasync
/await
, I'm bored of using callbacks, which are inconsistent with the rest of my codebaseWork so far
This is a work-in-progress pull request in order to get opinions on how people feel about this as a feature; and how it should be implemented.
So far, I have added a
promisify
method toutil
, which has the following signature:util.promisify = function(callback): { promise, callback }
If a
callback
is provided, thenpromise
isundefined
, and the returnedcallback
is the same as the providedcallback
. This is to avoid clients inadvertently creating logic forks, where code follows both a callback and aPromise
.If
callback
is not afunction
, then the returnedcallback
is afunction
that willresolve
orreject
the returnedpromise
.Also, for exiting functions early,
callback()
will return thepromise
, so you can write things like:if (condition) return callback();
.Discussion
Through this work I've discovered at least two points for discussion:
Doc
as it currently is means thatemit
is basically never called, becausecallback
will now always be defined. I don't know enough about how people use these events, but we could potentially have consumers configure ShareDB to enable promises? If promises are not enabled, thenutil.promisify
will always return the samecallback
as provided (even if it's falsy).Query
callback iscallback(error, results, extra)
, but aPromise
can onlyresolve
a single argument. Should we combine this into{ results, extra }
for the purposes of aPromise
?Promise
, or just not bother promisifying ifPromise
is undefined? (And if we avoid a polyfill, how do I get the linter to calm down without targeting ES6?)