-
-
Save younho9/5b36639fb845eabaaddad4f5dd150eed to your computer and use it in GitHub Desktop.
Mock HTTP request in javascript using promise and timeout.
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 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