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

@@ -18,12 +18,8 @@ function _accumulator<T>(
return new Transform({
...options,
transform(data: T, encoding, callback) {
try {
accumulateBy(data, buffer, this);
callback();
} catch (err) {
callback(err);
}
accumulateBy(data, buffer, this);
callback();
},
flush(callback) {
if (shouldFlush) {

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();
}
},
});

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();
},
});
}

View File

@@ -8,7 +8,6 @@ export function map<T, R>(
writableObjectMode: true,
},
): Transform {
// remove try catch
return new Transform({
...options,
async transform(chunk: T, encoding, callback) {

View File

@@ -20,14 +20,9 @@ export function parallelMap<T, R>(
}
inflight += 1;
callback();
try {
const res = await mapper(data);
this.push(res);
} catch (e) {
this.emit(e);
} finally {
inflight -= 1;
}
const res = await mapper(data);
this.push(res);
inflight -= 1;
},
async flush(callback) {
while (inflight > 0) {

View File

@@ -16,21 +16,8 @@ export function reduce<T, R>(
readableObjectMode: options.readableObjectMode,
writableObjectMode: options.writableObjectMode,
async transform(chunk: T, encoding, callback) {
let isPromise = false;
try {
const result = iteratee(value, chunk, encoding);
isPromise = result instanceof Promise;
value = await result;
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);
}
}
value = await iteratee(value, chunk, encoding);
callback();
},
flush(callback) {
// Best effort attempt at yielding the final value (will throw if e.g. yielding an object and