Last active
February 27, 2024 02:23
-
-
Save jofftiquez/c647654e76271119ea714805cfdb5ca2 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); | |
} | |
} |
xgqfrms
commented
Sep 8, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment