If you want to mock an API response in Javascript use Promise
with setTimeout
inside the function that will perform the call to the API like so:
return new Promise((resolve) => {
setTimeout(() => {
resolve(//insert what a successful response would be);
}, 2000);
});
We can also mock failing:
return new Promise((resolve, reject) => {
setTimeout(() => {
if(condition) {
resolve(//insert what a successful response would be);
}
else{
reject(//insert what an error response would be);
}
}, 2000);
})