This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calling from the prototype makes more sense than having to wrap a stream. Deprecates the `.toPromise(stream)` method.
- Loading branch information
Showing
3 changed files
with
29 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { reduce } from "./reduce"; | ||
import { PureStream } from "../PureStream"; | ||
import { reduce } from './reduce'; | ||
import { PureStream } from '../PureStream'; | ||
|
||
/** | ||
* Convert a stream into a promise for an array of values. | ||
* This consumes the stream. | ||
*/ | ||
export function toPromise<In, Out = In>(stream: PureStream<In, Out>) { | ||
// eslint-disable-next-line no-console | ||
console.trace( | ||
'Call to deprecated method `toPromise(stream)`; please use `PureStream.prototype.toPromise()`' | ||
); | ||
|
||
return new Promise<Out[]>((resolve, reject) => { | ||
stream.pipe(reduce<Out, Out[]>(async (prev, next) => { | ||
prev.push(next); | ||
return prev; | ||
}, [])) | ||
stream | ||
.pipe( | ||
reduce<Out, Out[]>(async (prev, next) => { | ||
prev.push(next); | ||
return prev; | ||
}, []) | ||
) | ||
.each(resolve) | ||
.done((err) => { | ||
if (err) reject(err); | ||
}) | ||
}); | ||
}); | ||
} |