Allow for once to return a promise#171
Conversation
To start some discussion regarding nodejs/node#20909 (comment)
|
I'm not a fan of this, apart from the simplified usage offered by |
|
I've been following the discussion around this, and while I applaud the effort modernize the EventEmitter pattern to become fully async aware I don't think that this is way to go about it. It is weird, that a single method of the EventEmitter is upgraded to support async, especially if this be solved in userland in 5~ LOC as well: function wonce(emitter, event) {
return new Promise(function (resolve) {
emitter.once(event, resolve)
})
}
const data = await wonce(server, 'connected');So for me this, the particular change would be -1, unless Node.js decides to implement it, then we will add support for it as well. Id be much more interested to see if we can make a fully async aware EventEmitter, instead of just spicing up a single method. Maybe work can be done around async iterators/generators for the for await (const data = stream.on('data')) {
...
}Or otherways to make EventEmitter fully async, instead of just a single method. |
The reason I am proposing this is because I've seen this seemingly simple implementation done wrong several times - when people have to write What do you think about my example here: nodejs/node#20909 (comment) and the discussion here: nodejs/node#20909 (comment) ?
Node.js 10 already ships with Quoting our docs: const fs = require('fs');
async function print(readable) {
readable.setEncoding('utf8');
let data = '';
for await (const k of readable) {
data += k;
}
console.log(data);
}
print(fs.createReadStream('file')).catch(console.log);This also has full backpressure support. |
|
I'm closing this, but we should add https://nodejs.org/api/events.html#events_events_once_emitter_name. |
To start some discussion regarding nodejs/node#20909 (comment)