Skip to content

Instantly share code, notes, and snippets.

@jportella93
Forked from arol/async.js
Last active July 25, 2018 22:16
Show Gist options
  • Save jportella93/13f8747ff5c551eaff52df2fa7eb2bbd to your computer and use it in GitHub Desktop.
Save jportella93/13f8747ff5c551eaff52df2fa7eb2bbd to your computer and use it in GitHub Desktop.
Asynchronicity exercise
// log the `rnd` result in the console using all three async techniques.
// You can only call console.log inside the `main` function.
const randomNumber = () => {
return Math.random();
}
// 1. Make it wait for 1 sec. with `setTimeout` and log it on main function
const timeoutRandomNumber = (func, cb) => {
return setTimeout(() => func(), 1000)
}
// 2. Now wrap the timeout version to work with promises
const promiseRandomNumber = () => {
}
// 3. Finally, code a final version with async await.
const asyncRandomNumber = () => {
}
const rangedRandomNumber = (base, min, max) => {
return Math.floor((base*(max-min))+min);
}
const main = () => {
const rnd = randomNumber()
// console.log(rangedRandomNumber(rnd, 14, 42));
console.log('randomNumber', randomNumber())
timeoutRandomNumber(randomNumber, (data) => console.log('timeoutRandomNumber', data))
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment