Last active
April 10, 2020 17:32
-
-
Save nonken/49e9995fda3ca1a8f45cfcec7f09e7b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Readable} from 'stream'; | |
class ReadableStream extends Readable { | |
constructor() { | |
super({objectMode: true}); | |
} | |
_read() { | |
this.emit('error', 'Error'); | |
} | |
} | |
// This is weird: Node doesn't die and also doesn't throw. | |
async function one() { | |
try { | |
const stream = new ReadableStream(); | |
for await (const chunk of stream) {} | |
} catch(e) { | |
// Doesn't get here | |
console.log(e) | |
} | |
} | |
one(); | |
// Error callback gets triggered as expected. | |
// Catch not reached as expected. | |
// But now we have a callback we need to handle somehow. | |
async function two() { | |
try { | |
const stream = new ReadableStream(); | |
stream.on('error', () => { | |
console.log('Gets here'); | |
}); | |
for await (const chunk of stream) { | |
} | |
} catch(e) { | |
// Doesn't get here | |
} | |
} | |
two(); | |
// Node dies. There is no point in wrapping this in an async function. | |
// We should use promises now and add an error handler. | |
async function three() { | |
try { | |
const stream = new ReadableStream(); | |
stream.on('data', () => { | |
// Gets here | |
console.log('Data'); | |
}); | |
// Node dies with unhandled error | |
} catch(e) { | |
// Doesn't get here | |
} | |
} | |
three(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment