Says, I have an api call that take an input since
which is Unix timestamp.
fetchData = (since?) => {
const now = Math.floor(Date.now() / 1000); // Current Unix timestamp in seconds
if (since) {
//handle logic
console.log(since)
}
return of({ since: now });
};
Now I want to make an infinite loop that in each call, the input value to fetchData is the return value from the previous call. I tried with this code:
fetchData().pipe(
expand((response: any) => {
const { since } = response;
return fetchData(since);
}),
delay(3000),
repeat(),
);
I don’t know why console.log kept giving me the same value. What have I done wrong here?
2
Answers
You can use
interval
to create the infinite loop and then switch to the inner fetchData usingswitchMap
to get the desired result.Stackblitz Demo
You can do it like this:
Stackblitz Demo