DEV Community

Discussion on: Why not to use setInterval

Collapse
stevetaylor profile image
Steve Taylor

There are perfectly sane ways of using setInterval without requests backing up, e.g. (warning: untested RxJS code ahead):

interval(10000)
    .switchMap(() => Observable.create(observer => {
        const request = superagent.get(
            'https://example.com/api/v1/foo',
            (err, res) => {
                if (!err) {
                    observer.next(res.body)
                }

                observer.complete()
            }
        )

        return () => request.abort()
    }))
    .subscribe(console.log)
Enter fullscreen mode Exit fullscreen mode