diff --git a/1-js/11-async/08-async-await/04-still-a-promise/solution.md b/1-js/11-async/08-async-await/04-still-a-promise/solution.md new file mode 100644 index 0000000000..3de50298a4 --- /dev/null +++ b/1-js/11-async/08-async-await/04-still-a-promise/solution.md @@ -0,0 +1,74 @@ + +# Boring times + +You may have been tempted to take the lazy, slow, boring pseudo-synchronous way. + +It's ok... + +```js + +// Your code +// + +async function showTimes() { + const time1 = await babieca.run(); + alert(time1); + + const time2 = await rocinante.run(); + alert(time2); + + const time3 = await bucephalus.run(); + alert(time3); +} + +showTimes() + +``` + +No much fun. + +There is a better way. Make use of the promise API. + + +# Let's race! + +```js run +class Horse { + constructor(name) { + this.name = name; + } + + async run() { + const time = Math.random() * 30 + 10; // 10 to 40 seconds + + await new Promise(resolve => setTimeout(resolve, time * 1000 / 20)); // 20x. We don't want to wait realistic times, do we? + + const result = `${time.toFixed(2)} seconds for ${this.name}!!! `; // name, seconds.hundreths + console.log(result); + return result; + } +} + +const babieca = new Horse('Babieca'); +const rocinante = new Horse('Rocinante'); +const bucephalus = new Horse('Bucephalus'); + +// Your code... +// + +async function race() { + const results = await Promise.all([ + babieca.run(), + rocinante.run(), + bucephalus.run() + ]); + + alert("All the horses have reached the goal! 🎉 \n" + results.join('\n')); +} + +race(); + +``` + +This has no cost for your code. The horses run simultaneously. You may see as they are arriving in your console. + diff --git a/1-js/11-async/08-async-await/04-still-a-promise/task.md b/1-js/11-async/08-async-await/04-still-a-promise/task.md new file mode 100644 index 0000000000..e58202a1aa --- /dev/null +++ b/1-js/11-async/08-async-await/04-still-a-promise/task.md @@ -0,0 +1,31 @@ + +# Still a promise + +Make the 3 horses run then show their times + +```js + +class Horse { + constructor(name) { + this.name = name; + } + + async run() { + const time = Math.random() * 30 + 10; // 10 to 40 seconds + + await new Promise(resolve => setTimeout(resolve, time * 1000 / 20)); // 20x. We don't want to wait realistic times, do we? + + const result = `${time.toFixed(2)} seconds for ${this.name}!!! `; // name, seconds.hundreths + console.log(result); + return result; + } +} + +const babieca = new Horse('Babieca'); +const rocinante = new Horse('Rocinante'); +const bucephalus = new Horse('Bucephalus'); + +// Your code... +// + +``` diff --git a/1-js/11-async/08-async-await/05-there-can-be-only-one/solution.md b/1-js/11-async/08-async-await/05-there-can-be-only-one/solution.md new file mode 100644 index 0000000000..3a0a65889b --- /dev/null +++ b/1-js/11-async/08-async-await/05-there-can-be-only-one/solution.md @@ -0,0 +1,41 @@ +Let's race! + +```js run +class Horse { + constructor(name) { + this.name = name; + } + + async run() { + const time = Math.random() * 30 + 10; // 10 to 40 seconds + + await new Promise(resolve => setTimeout(resolve, time * 1000 / 20)); // 20x. We don't want to wait realistic times, do we? + + const result = `${time.toFixed(2)} seconds for ${this.name}!!! `; // name, seconds.hundreths + console.log(result); + return result; + } +} + +const babieca = new Horse('Babieca'); +const rocinante = new Horse('Rocinante'); +const bucephalus = new Horse('Bucephalus'); + +// Your code... +// + +async function race() { + const fastest = await Promise.any([ + babieca.run(), + rocinante.run(), + bucephalus.run() + ]); + + alert(`We have a winner! : ${fastest}`); + // Fun fact: slower horses will continue running inside the engine, but nobody cares anymore + +} + +race(); + +``` diff --git a/1-js/11-async/08-async-await/05-there-can-be-only-one/task.md b/1-js/11-async/08-async-await/05-there-can-be-only-one/task.md new file mode 100644 index 0000000000..aae4d3cb16 --- /dev/null +++ b/1-js/11-async/08-async-await/05-there-can-be-only-one/task.md @@ -0,0 +1,31 @@ + +# There can be only one + +Make the 3 horses run, show only the winner + +```js + +class Horse { + constructor(name) { + this.name = name; + } + + async run() { + const time = Math.random() * 30 + 10; // 10 to 40 seconds + + await new Promise(resolve => setTimeout(resolve, time * 1000 / 20)); // 20x. We don't want to wait realistic times, do we? + + const result = `${time.toFixed(2)} seconds for ${this.name}!!! `; // name, seconds.hundreths + console.log(result); + return result; + } +} + +const babieca = new Horse('Babieca'); +const rocinante = new Horse('Rocinante'); +const bucephalus = new Horse('Bucephalus'); + +// Your code... +// + +``` pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy