/**
* Examples:
*
* fetchFakeApi({data: [1,2,3], delay: 1000}).then(console.log)
* => [1,2,3]
*
* fetchFakeApi({data: [1,2,3], delay: 1000, error: true}).then(console.log)
* => Uncaught (in promise) {error: "An error has occurred while fetching"}
*
* fetchFakeApi({data: [1,2,3], time: 1000, error: {msg: "Override error message" }}).then(console.log)
* => Uncaught (in promise) {error: "Override error message"}
*/
function fetchFakeApi({
data = {},
delay = 3000,
error
}) {
return new Promise((resolve, reject) => {
if (!error) {
setTimeout(resolve.bind(null, data), delay);
} else {
setTimeout(
reject.bind(null, {
error: error.msg
? error.msg
: "An error has occurred while fetching"
}),
delay
);
}
});
}