Last active
November 21, 2019 20:24
-
-
Save HichemBenChaaben/e69ae2c2ede06e987907849ad0aad3fe to your computer and use it in GitHub Desktop.
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 { getRandomWordSync, getRandomWord } = require("word-maker"); | |
console.log("It works!"); | |
// 1 | |
[...Array(100)].map((_, index) => | |
console.log(`${index + 1}: ${getRandomWordSync()}`) | |
); | |
// 2 | |
const fizzBuzz = (x, word) => | |
x % 15 === 0 | |
? "fizzBuzz" | |
: x % 3 === 0 | |
? "fizz" | |
: x % 5 === 0 | |
? "buzz" | |
: word; | |
[...Array(100)].map((_, index) => | |
console.log(`${index + 1}: ${fizzBuzz(index + 1, getRandomWordSync())}`) | |
); | |
// 3 | |
[...Array(100)].map(async (_, index) => { | |
const word = await getRandomWord(); | |
console.log(`${index + 1}: ${fizzBuzz(index + 1, word)}`); | |
}); | |
// 4 | |
[...Array(100)].map((_, index) => { | |
try { | |
const word = getRandomWordSync({ withErrors: true }); | |
console.log(`${index + 1}: ${word}`); | |
} catch (err) { | |
console.log(`${index + 1}: i don't want to break anything`); | |
} | |
}); | |
[...Array(100)].map(async (_, index) => { | |
try { | |
const word = await getRandomWord({ withErrors: true }); | |
console.log(`${index + 1}: ${fizzBuzz(index + 1, word)}`); | |
} catch (err) { | |
console.log(`${index + 1}: i don't want to break anything`); | |
} | |
}); | |
// 5 | |
console.time("Bonus stage"); | |
const tasks = [...Array(100)].map((_, index) => | |
getRandomWord({ slow: true, withErrors: true }) | |
.then(word => `${index + 1}: ${fizzBuzz(index + 1, word)}`) | |
.catch(() => `${index + 1}: i don\'t want to break anything`) | |
); | |
tasks | |
.reduce((acc, fn) => { | |
return acc.then(res => fn.then(curr => [...res, curr])); | |
}, Promise.resolve([])) | |
.then(res => res.map(r => console.log(r))); | |
console.timeEnd("Bonus stage"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment