Skip to content

Instantly share code, notes, and snippets.

@NitsanBaleli
Last active February 6, 2018 13:15
Show Gist options
  • Save NitsanBaleli/f650d6a3504f7ac64e0b735ecc71bb51 to your computer and use it in GitHub Desktop.
Save NitsanBaleli/f650d6a3504f7ac64e0b735ecc71bb51 to your computer and use it in GitHub Desktop.
Loop ajax requests with Rxjs
var array = ["[email protected]", "[email protected]"];
const emails = () => array;
module.exports = emails;
const Rx = require('rxjs/Rx');
const axios = require('axios');
const emails = require('./emails');
let array = emails();
let baseUrl = "https://api-staging.antelopesystem.com/SignalsServer/users/forgotpassword?appId=1&email=";
//we use flatMap becuase order doesnt matter
//https://www.learnrxjs.io/operators/transformation/mergemap.html
var source = Rx.Observable.from(array).flatMap((email) => {
return Rx.Observable.fromPromise(axios.get(`${baseUrl}${email}`).then((res) => {
console.log(`completed with ${email}`);
return res;
})).delay(30)
});
var subscription = source.subscribe(
(x) => {},
(err) => {},
() => console.log('Completed'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment