Skip to content

Instantly share code, notes, and snippets.

@nickcharlton
Created April 19, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcharlton/9ce9d6685af284c36380a9d4132fc1c1 to your computer and use it in GitHub Desktop.
Save nickcharlton/9ce9d6685af284c36380a9d4132fc1c1 to your computer and use it in GitHub Desktop.
Node: Synchronous/Blocking HTTP Request
// 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