Skip to content

Instantly share code, notes, and snippets.

@manjufy
Created June 14, 2018 02:46
Show Gist options
  • Save manjufy/22957e386575be5e84f78122b36f85e4 to your computer and use it in GitHub Desktop.
Save manjufy/22957e386575be5e84f78122b36f85e4 to your computer and use it in GitHub Desktop.
Mocking axios
let axios = { // mocks
get: function(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve({data: x})
}, 2000)
})
}}
let query = 'mangos'
async function fetchData(query) {
try {
const response = await axios.get(`/q?query=${query}`)
const data = response.data
return data
} catch (error) {
console.log(error)
}
}
fetchData(query).then(data => {
console.log(data) // Got data 2s later... Can use data!
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment