-
-
Notifications
You must be signed in to change notification settings - Fork 6
How promise APIs would look like in core #16
Description
Proposing this thread to consolidate the various discussions and proposals around "How promise APIs would look like in core." as the original pull request covers more than just that sole issue, making it hard to keep track of what is actually being proposed. Feel free to edit. [CD: Thanks!]
Proposals for current APIs:
Active proposals:
require('fs').readFilePromise(current PR proposal) +require('fs').promised.readFileshortcut
Adding Core support for Promises node#5020 (comment)require('fs/promise').readFile(future PR proposal) (to immediately follow 5020 once landed)
Current / unresolved discussions:
- do any of the active proposals support callbacks at all? if so, when, why and how? if not, why or when?
require('fs/promise').readFileinitially, orrequire('fs').readFilePromiseinitially- arguments regarding
require('fs/promise').readFileinitially and perhaps only- +1 works better with ES6 imports Adding Core support for Promises node#5020 (comment)
+1 allows blacklisting How promise APIs would look like in core #16 (comment)[CD: For top level methods, yes, but not for class instance methods. Still best to block promises usingglobal.Promise = nullin all cases.]-1 requires PR to be updated How promise APIs would look like in core #16 (comment)[CD: I am not concerned about changing the PR.]- [CD: my concerns with blocking the merge of 5020 on this approach.]
- -1 unclear on what to do for instance methods (
net.Socket#setTimeout) - -1 unclear on whether or not to re-export non-promise-returning methods (
fs.createReadStream) - -1 difficult to drive consensus within collaborator group on whether or not to add submodule
- -1 difficult to drive consensus on what to call the module (
promisedis a frontrunner, since we have the name, but this is a potential for bikeshedding)
- -1 unclear on what to do for instance methods (
- arguments regarding
require('fs').readFilePromisewith or withoutrequire('fs').promised.readFileshortcut initially- -1 doesn't work well with ES6 imports Adding Core support for Promises node#5020 (comment)
- [CD: the
promisedshortcut works for destructuring assign,import x as ysyntax should work for ES2015 modules].
- [CD: the
- -1 despite being in the PR, no consensus has formed yet, also if support is added for all three (not just
require('fs/promise')then maintaining 3 endpoints that all do the same thing is pain Adding Core support for Promises node#5020 (comment) - +1 already implemented in the PR so can be merged in sooner How promise APIs would look like in core #16 (comment)
- +1 solution extends to methods of class instances
- -1 doesn't work well with ES6 imports Adding Core support for Promises node#5020 (comment)
- are there any arguments differentiating between
require('fs').readFilePromiseandrequire('fs').promised.readFile, why one or the other?
- arguments regarding
- should promises wrap callback functions, or should callback functions wrap promise functions (initially or later)
Closed proposals:
- Promise only:
require('fs').readFilereturns promises only, no support for callback — one API endpoint to maintain, b/c break for everyone, consistent return types, promises are primary and callbacks are eliminated, could provide cleanest forward API- argument against here How promise APIs would look like in core #16 (comment) b/c break is just too intense
- Callback or promise:
require('fs').readFilereturns promise if no callback is provided, if callback is provided do not return promise and use callback — one API endpoint to maintain, performance hit with additional if check, different return types, callbacks or promises become secondary to the other- argument against here Adding Core support for Promises node#5020 (comment) as it overloads return values
- argument against here Adding Core support for Promises node#5020 (comment) as it will cause issues with things like
process.send(message[, sendHandle][, callback])which can have a return value despite having an async callback, changing return value to promise will break b/c with current return value
- Promise always with callback support:
require('fs').readFilereturns promise always, if callback is provided attach it to the promise — one API endpoint to maintain, performance hit for callback users, consistent return types, promises are primary and callbacks become secondary- argument against here Adding Core support for Promises node#5020 (comment) as it will cause b/c breaks with things like
process.send(message[, sendHandle][, callback])which can have a return value despite having an async callback, changing return value to promise will break b/c with current return value
- argument against here Adding Core support for Promises node#5020 (comment) as it will cause b/c breaks with things like
Proposals for error APIs:
Discussion ongoing at #10, summary:
[CD - There may not need to be an error API — it might look like a flag, like --abort-on-sync-rejection]
// https://github.com/nodejs/promises/issues/10#issue-133802245
// recovery object
// [CD - It appears likely that we'll go this route since error symposium folks have raised concerns about it.]
await fs.readFilePromise('some/file', {
ENOENT() {
return null
}
})
// normal use:
fs.readFilePromise('some/file')// https://github.com/nodejs/promises/issues/10#issuecomment-184375707
// [CD - It's unlikely that we'll go this route because I'd like to avoid subclassing Promise.]
let file = await fs.getFileAsync('dne').recover({
EISDIR(err) {}
ENOENT(err) { ... }
});Proposals for new APIs:
[CD - these probably will not land in nodejs/node#5020].
util.promised:util.promised- Callbacks and async/await #5util.toCallback:util.toCallback(asyncFn)- interop of async functions with callbacks. #6
The other issue "How to make sure promises don't get in the way of callback users with core support." seems like a sub-topic of "How promise APIs would look like in core". So perhaps that should be consolidated here too. [CD - Promises will be added using a promisify wrapper, without affecting the original function. They should not interfere with callback APIs at all.]