Created
April 19, 2018 09:54
-
-
Save nickcharlton/9ce9d6685af284c36380a9d4132fc1c1 to your computer and use it in GitHub Desktop.
Node: Synchronous/Blocking HTTP Request
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
// In the rare case you actually want a syncronous request | |
// this will wait until the data arrives before continuing | |
// down the file. | |
// | |
// The service where this is used fetches configuration data | |
// from an external service in order to run. | |
const axios = require('axios') | |
let done = false | |
let data | |
console.log('making request') | |
axios.get('https://httpstat.us/200').then((response) => { | |
console.log('completed request') | |
data = response.data | |
done = true | |
return data | |
}).catch((error) => { | |
console.log(error) | |
throw error | |
}) | |
require('deasync').loopWhile(() => { return !done }) | |
console.log('continuing execution') | |
console.log(data) | |
// Output: | |
// making request | |
// completed request | |
// continuing execution | |
// 200 OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment