Created
August 13, 2017 05:31
-
-
Save spunkedy/27aa9d3db6abdcb401f75c67da8be8e3 to your computer and use it in GitHub Desktop.
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
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); | |
} | |
}); |
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
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); | |
} |
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
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