Created
June 14, 2018 02:46
-
-
Save manjufy/22957e386575be5e84f78122b36f85e4 to your computer and use it in GitHub Desktop.
Mocking axios
This file contains hidden or 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
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