Skip to content

Instantly share code, notes, and snippets.

@dakshgautam1
Created July 3, 2018 19:20
Show Gist options
  • Save dakshgautam1/a6907c903abaab1250a20fdf9bc1b8ca to your computer and use it in GitHub Desktop.
Save dakshgautam1/a6907c903abaab1250a20fdf9bc1b8ca to your computer and use it in GitHub Desktop.
A quick way to mock api call.
const fakeDatabase = option => {
// add case according to the option.
switch (option) {
case 1:
return [{
name: 'James Bond Singh',
age: 41
}, {
name: 'Beauty Kumar',
age: 12
}]
case 2:
return {
name: 'Beauty Kumar',
age: 12,
hobbies: ['Talking', 'Sleeping']
}
default:
return []
}
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// api call mocking server latency
const fakeApiCall = option =>
delay(2000).then(() => {
return fakeDatabase(option)
});
fakeApiCall(1).then(result => console.log(result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment