【发布时间】:2019-05-15 00:05:23
【问题描述】:
我正在编写一个每隔一定秒数轮询服务器的史诗,在这种情况下,3 秒启动延迟,每 5 秒轮询一次,通过管道使用计时器,但我不确定这种行为背后的原因.
我的史诗不是在等待我的内心完成,所以输出真的很奇怪。
我尝试过更改地图并使用exhaustMap,但不确定我是否在正确的树上吠叫。
export const testingEpics = action$ => {
// Stop upon a end pending action trigger, for debugging/stopping if needed
const stopPolling$ = action$.pipe(ofType(END_PENDING_ACTIONS));
return action$.pipe(
// On begin pending actions
ofType(BEGIN_PENDING_ACTIONS),
switchMap(action =>
// At a 5 second interval
timer(3 * 1000, 5 * 1000).pipe(
// Stop when epics see a end pending action
takeUntil(stopPolling$),
switchMap(() =>
// Get the db
from(getDb()).pipe(
mergeMap(db => {
console.log('Run again!!');
return from(
new Promise(resolve => setTimeout(resolve, 10000))
).pipe(
// what happens if action is still running but no internet?
// delay(9.9 * 1000),
// actions is an array from the db
// switchmap at top is reason for it, handle for future. lol
map(actions => console.log('Hello world'))
);
})
)
)
)
)
);
};
预期的结果将是
(Initial delay 3 seconds)
Run Again!!
(Wait's 10 seconds for inner promise to complete)
Hello world
(Subsequent delay of 5 seconds)
Run Again!!
(Wait's another 10 seconds for inner promise to complete)
Hello world
(Subsequent delay of 5 seconds)
Run Again!!
(Wait's another 10 seconds for inner promise to complete)
Hello world
(Subsequent delay of 5 seconds)
.
.
.
【问题讨论】:
-
getDb返回什么?这实际上是调用你的服务器,还是new Promise模拟了对服务器的调用?
标签: javascript rxjs redux-observable