I have an ES6 promise. I would like to use the promise to serialize API requests. There are interactive requests and background requests.
var apiPromise = Promise.resolve();
...
// Interactive requests
router.get('/apiRequest', (req,res) => {
apiPromise.then(doSomeStuff)
.then((results) => {
res.send(results)
})
...
// Background requests
setInterval(() => {
if (apiPromise is resolved) {
apiPromise.then(doSomeStuff)
}
}, 1000*60)
I'd like to setInterval a function to .then() a queued request, only if the promise has been resolved. If it's not resolved, just wait to check at the next interval.
What's the best way to do that?
Update: Bluebird.js offers synchronous inspection: http://bluebirdjs.com/docs/api/isfulfilled.html