Add last() method

This commit is contained in:
Sami Turcotte
2018-12-02 21:53:10 -05:00
parent cd6fccd925
commit f630379c24
2 changed files with 25 additions and 0 deletions

View File

@@ -427,3 +427,16 @@ export function duplex(writable: Writable, readable: Readable) {
export function child(childProcess: ChildProcess) {
return duplex(childProcess.stdin, childProcess.stdout);
}
/**
* Resolve the last streamed chunk of the given readable stream, after it has ended
* @param readable The readable stream to wait on
*/
export function last<T>(readable: Readable): Promise<T | null> {
let lastChunk: T | null = null;
return new Promise((resolve, reject) => {
readable
.on("data", chunk => (lastChunk = chunk))
.on("end", () => resolve(lastChunk));
});
}