I have this:
var bulk = array[1,2,3,4]
var finalResult = Bacon
.fromArray(bulk)
.flatMap(isValInCouchDb)
.filter(onesThatExist)
.flatMap(putValInCouchDb)
I need right after the filter to delay the whole stream by 1 second and then run putValInCouchDb Something like this really:
var bulk = array[1,2,3,4]
var finalResult = Bacon
.fromArray(bulk)
.flatMap(isValInCouchDb)
.filter(onesThatExist)
.delay(1000)
.flatMap(putValInCouchDb)
But I think I'm missing something because .delay(1000) is delaying everything for as many seconds as there are items and then runs them all once all the time has lapsed.
Any ideas ?
UPDATE
Just to clarify based on the answer from Alex
What I would need is:
1 "filter" 1
2 "filter" 2
3 "filter" 3
4 "filter" 4
[waits 1 second]
1004 "flatMap" 1
[waits 1 second]
2004 "flatMap" 2
[waits 1 second]
3005 "flatMap" 3
[waits 1 second]
4006 "flatMap" 4
UPDATE 2 - With Solution Used
From the answer from Bergi, using .zip with an interval actually worked but what happens with .zip is that .interval creates an event every second and then each event from .interval waits for each event from my database checks. A lot of database checks happen in bulk and very fast so 'excessive buffering' occurs (which is what the bacon docs warn you).
So I decided to do it this way instead:
var bulk = array[1,2,3,4]
var finalResult = Bacon
.fromArray(bulk)
.flatMap(isValInCouchDb)
.filter(onesThatExist)
.fold([], function(a, b){ a.push(b); return a })
.flatMap(function(a){return Bacon.sequentially(1500, a)})
.flatMap(putValInCouchDb)
This worked nicely since folding the filtered results and sequentially creating events from them is natural, very descriptive, and without side effects.
Bacon is quite awesome