Skip to content

Instantly share code, notes, and snippets.

@macghriogair
Created September 8, 2017 07:57
Show Gist options
  • Save macghriogair/b5f9018674c6b52595630a21090fa9d5 to your computer and use it in GitHub Desktop.
Save macghriogair/b5f9018674c6b52595630a21090fa9d5 to your computer and use it in GitHub Desktop.
javascript polling w\ Promise
pollProgress(timeout, interval) {
let endTime = Number(new Date()) + (timeout || 3 * 1000 * 60)
interval = interval || 1000
let checkCondition = (resolve, reject) => {
this.fetchProgress(this.progressToken)
.then((response) => {
if (response.data.percentage >= 100) {
resolve(response)
} else if (Number(new Date()) < endTime) {
setTimeout(checkCondition, interval, resolve, reject)
} else {
reject(new Error('Poll timed out: ' + arguments))
}
}).catch(e => reject(e))
}
return new Promise(checkCondition)
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment