Created
April 28, 2019 17:00
-
-
Save kosich/4efcae7ff575a3382a9ece0dbf77154b to your computer and use it in GitHub Desktop.
retryWhen with promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { rxObserver } = require('api/v0.3'); | |
const { defer } = require('rxjs'); | |
const { retryWhen, tap, delay } = require('rxjs/operators'); | |
defer(() => mockRequest()).pipe( | |
retryWhen(errors$ => | |
errors$.pipe( | |
tap(rxObserver('retries')), | |
delay(10) | |
) | |
) | |
) | |
.subscribe(rxObserver('result')); | |
function mockRequest(){ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(() => { | |
if (Date.now() > 100) { | |
resolve('OK'); | |
} else { | |
reject('FAIL'); | |
} | |
}, 10); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment