Created
July 1, 2023 03:30
-
-
Save spencerfeng/53d53a3ff1bf91c8c621cf0bc9d91177 to your computer and use it in GitHub Desktop.
For tutorial: Testing JavaScript Code with Timeouts and Promises
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
export const fetchMessage = () => { | |
const messages = ["Message 1", "Message 2", "Message 3"] | |
const randomIndex = Math.floor(Math.random() * messages.length); | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(messages[randomIndex]) | |
}, 2000) | |
}) | |
} |
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
import { fetchMessage } from "./fetchMessage.js" | |
export const handleFetchedMessage = (callback) => { | |
setTimeout(async () => { | |
const fetchedMessage = await fetchMessage() | |
callback(fetchedMessage) | |
}, 1000000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment