Skip to content

Instantly share code, notes, and snippets.

@spunkedy
Created August 13, 2017 05:31
Show Gist options
  • Save spunkedy/27aa9d3db6abdcb401f75c67da8be8e3 to your computer and use it in GitHub Desktop.
Save spunkedy/27aa9d3db6abdcb401f75c67da8be8e3 to your computer and use it in GitHub Desktop.
import longPoll from './lib/longPoll';
longPoll("http://example.com/livereload", (err,res) => {
if(err){
console.log("error");
console.log(err);
} else {
console.log("reloading");
console.log(res);
}
});
import request from './request';
export default (urlToPoll,callback) => {
let makeRequest = () =>{
request({
url:urlToPoll,
timeout: 1000
})
.then((data)=> {
callback(null,data);
})
.catch(callback);
}
makeRequest();
setInterval(makeRequest,10000);
}
export default (props = {} ) => {
return new Promise((resolve,reject) => {
const XHR = new XMLHttpRequest();
// Configuring XHR instance to load data that we need.
XHR.open('GET', props.url);
if( props.timeout ){
XHR.timeout = props.timeout;
}
XHR.addEventListener('abort', reject);
// Adding event listener to retreive data when it will be loaded.
XHR.addEventListener('load', event => {
// Parsing request response to JSON and resolving promise
resolve(JSON.parse(event.target.responseText));
});
XHR.addEventListener('error', reject);
// Initiating request.
XHR.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment