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

@@ -9,23 +9,11 @@ export function filter<T>(
return new Transform({
...options,
async transform(chunk: T, encoding?: any, callback?: any) {
let isPromise = false;
try {
const result = predicate(chunk, encoding);
isPromise = result instanceof Promise;
if (!!(await result)) {
callback(null, chunk);
} else {
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);
}
const result = await predicate(chunk, encoding);
if (result === true) {
callback(null, chunk);
} else {
callback();
}
},
});