Zipping value with corresponding FlatMap result
Let's say I have a stream of files filesStream
and a function uploadFile
that returns a stream of one value (Rx.Observable.fromPromise (...)). Files in a stream can be uploaded with a simple one flatMap
:
filesStream.flatMap(uploadFile)
I need to zip files from filesStream
with the corresponding answers from uploadFile
:
zippedStream.subscribe(
(file, response) => console.log("File " + file.name + " uploaded: " + response.message)
)
I came up with a rather dirty approach that works
var zipppedStream = filesStream.flatMap(
(file) => uploadFile(file).zip(Rx.Observable.just(file), (r, f) => [r, f])
)
But I don't really like this as I need to unpack a two-digit array into subscribe
and it usually looks heavy. Is this how you do it or am I missing something?
source to share
You can work around with zip
and use overload flatMap
instead to accomplish what you want here
let zippedStream = fileStream.flatMap(
(file) => uploadFile(file),
(file, uploaded) => [file, uploaded]);
zippedStream.subscribe(
([file, response]) => console.log("File " + file.name + " uploaded: " + response.message));
source to share