Skip to content

Instantly share code, notes, and snippets.

@alexHlebnikov
Forked from jofftiquez/mock-http.js
Created March 20, 2023 17:45
Show Gist options
  • Save alexHlebnikov/ce667c2869ea6c57dd0864982f2a2fdb to your computer and use it in GitHub Desktop.
Save alexHlebnikov/ce667c2869ea6c57dd0864982f2a2fdb to your computer and use it in GitHub Desktop.
Mock HTTP request in javascript using promise and timeout.
const mock = (success, timeout = 1000) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(success) {
resolve();
} else {
reject({message: 'Error'});
}
}, timeout);
});
}
const someEvent = async () => {
try {
await mock(true, 1000);
} catch (e) {
console.log(e.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment