Skip to content

Instantly share code, notes, and snippets.

@younho9
Forked from jofftiquez/mock-http.js
Created July 2, 2021 01:06
Show Gist options
  • Save younho9/5b36639fb845eabaaddad4f5dd150eed to your computer and use it in GitHub Desktop.
Save younho9/5b36639fb845eabaaddad4f5dd150eed to your computer and use it in GitHub Desktop.
Mock HTTP request in javascript using promise and timeout.
const mock = (success, timeout) => {
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