-
-
Save alexHlebnikov/ce667c2869ea6c57dd0864982f2a2fdb 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 = 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