Created
July 3, 2018 19:20
-
-
Save dakshgautam1/a6907c903abaab1250a20fdf9bc1b8ca to your computer and use it in GitHub Desktop.
A quick way to mock api call.
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
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