Remove try catch from provided functions, user handles errors

This commit is contained in:
Jerry Kurian
2019-09-12 09:41:04 -04:00
parent 586f618e95
commit 517e281ce5
11 changed files with 26 additions and 74 deletions

View File

@@ -13,21 +13,8 @@ export function flatMap<T, R>(
return new Transform({
...options,
async transform(chunk: T, encoding, callback) {
let isPromise = false;
try {
const mapped = mapper(chunk, encoding);
isPromise = mapped instanceof Promise;
(await mapped).forEach(c => this.push(c));
callback();
} catch (err) {
if (isPromise) {
// Calling the callback asynchronously with an error wouldn't emit the error, so emit directly
this.emit("error", err);
callback();
} else {
callback(err);
}
}
(await mapper(chunk, encoding)).forEach(c => this.push(c));
callback();
},
});
}