From 5ef6ae170db2cfd8f307e4ba5d28d4d4e9f0a07f Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Tue, 5 Nov 2024 23:20:22 -0300 Subject: [PATCH 1/8] Bosquejo inicial- probar- ojo necesita agregar codigo a sol1, 2 --- .../04-still-a-promise/solution.md | 23 +++++++++++++++ .../04-still-a-promise/solution2.md | 28 +++++++++++++++++++ .../08-async-await/04-still-a-promise/task.md | 28 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 1-js/11-async/08-async-await/04-still-a-promise/solution.md create mode 100644 1-js/11-async/08-async-await/04-still-a-promise/solution2.md create mode 100644 1-js/11-async/08-async-await/04-still-a-promise/task.md 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..949eaffe3f --- /dev/null +++ b/1-js/11-async/08-async-await/04-still-a-promise/solution.md @@ -0,0 +1,23 @@ + +You may be tempted to take the lazy, slow, boring pseudo-synchronous way. + +It's ok... + +```js run + + +async function showTimes() { + const time1 = await babieca.run(); + alert(time1); + + const time2 = await rocinante.run(); + alert(time2); + + const time3 = await belcebu.run(); + alert(time3); +} + +``` + +No much fun. +There is a better way. Use the promise API diff --git a/1-js/11-async/08-async-await/04-still-a-promise/solution2.md b/1-js/11-async/08-async-await/04-still-a-promise/solution2.md new file mode 100644 index 0000000000..ffe1916835 --- /dev/null +++ b/1-js/11-async/08-async-await/04-still-a-promise/solution2.md @@ -0,0 +1,28 @@ + +Let's race! + +```js run + + +async function race() { + const results = await Promise.all([ + babieca.run(), + rocinante.run(), + belcebu.run() + ]); + + alert("All the horses reached the goal! πŸŽ‰πŸ‡\n" + results.join('\n')); +} + +race(); + +``` + +This has no cost for your code. The horses run simultaneously. You may see when they are arriving in your console. + + +Please note: if you only care for the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones + + const fastest = await Promise.any([babieca.run(), rocinante.run(), belcebu.run()]); + alert(`The winner: ${fastest}`); + 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..b794e2d660 --- /dev/null +++ b/1-js/11-async/08-async-await/04-still-a-promise/task.md @@ -0,0 +1,28 @@ + +# Still a promise + +Make the horses run then show their times + +```js + +class Horse { + constructor(name) { + this.name = name; + } + + async run() { + const time = Math.floor(Math.random() * 3) + 1; + + await new Promise(resolve => setTimeout(resolve, time * 1000)); + + const result = `${time * 20} segundos para ${this.name}!!! `; + console.log(result); + return result; + } +} + +const babieca = new Horse('Babieca'); +const rocinante = new Horse('Rocinante'); +const belcebu = new Horse('BelcebΓΊ'); + +``` From 7d8b7f5224255d2aea4973b9a9be58a333bdf716 Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Tue, 5 Nov 2024 23:43:40 -0300 Subject: [PATCH 2/8] oops --- 1-js/11-async/08-async-await/04-still-a-promise/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index b794e2d660..115b9855f2 100644 --- 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 @@ -23,6 +23,6 @@ class Horse { const babieca = new Horse('Babieca'); const rocinante = new Horse('Rocinante'); -const belcebu = new Horse('BelcebΓΊ'); +const bucephalus = new Horse('Bucephalus'); ``` From 37abe8cfba3be61c395635673302c6a586a0796a Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Tue, 5 Nov 2024 23:44:15 -0300 Subject: [PATCH 3/8] Update solution.md --- 1-js/11-async/08-async-await/04-still-a-promise/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 949eaffe3f..8f55355cd3 100644 --- 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 @@ -13,7 +13,7 @@ async function showTimes() { const time2 = await rocinante.run(); alert(time2); - const time3 = await belcebu.run(); + const time3 = await bucephalus.run(); alert(time3); } From 8d0d84dc5f76d39305c08b8954ec30a486b1ed8b Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Tue, 5 Nov 2024 23:44:51 -0300 Subject: [PATCH 4/8] Update solution2.md --- 1-js/11-async/08-async-await/04-still-a-promise/solution2.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/11-async/08-async-await/04-still-a-promise/solution2.md b/1-js/11-async/08-async-await/04-still-a-promise/solution2.md index ffe1916835..5fdca165a7 100644 --- a/1-js/11-async/08-async-await/04-still-a-promise/solution2.md +++ b/1-js/11-async/08-async-await/04-still-a-promise/solution2.md @@ -8,7 +8,7 @@ async function race() { const results = await Promise.all([ babieca.run(), rocinante.run(), - belcebu.run() + bucephalus.run() ]); alert("All the horses reached the goal! πŸŽ‰πŸ‡\n" + results.join('\n')); @@ -23,6 +23,6 @@ This has no cost for your code. The horses run simultaneously. You may see when Please note: if you only care for the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones - const fastest = await Promise.any([babieca.run(), rocinante.run(), belcebu.run()]); + const fastest = await Promise.any([babieca.run(), rocinante.run(), bucephalus.run()]); alert(`The winner: ${fastest}`); From d7012edc25cf9678e5051d5e9d127b6e44143f81 Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Mon, 27 Jan 2025 00:20:16 -0300 Subject: [PATCH 5/8] Two solutions one file --- .../04-still-a-promise/solution.md | 37 ++++++++++++++++++- .../04-still-a-promise/solution2.md | 28 -------------- .../08-async-await/04-still-a-promise/task.md | 9 +++-- 3 files changed, 40 insertions(+), 34 deletions(-) delete mode 100644 1-js/11-async/08-async-await/04-still-a-promise/solution2.md 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 index 8f55355cd3..e634025776 100644 --- 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 @@ -1,5 +1,7 @@ -You may be tempted to take the lazy, slow, boring pseudo-synchronous way. +# Boring times + +You may have been tempted to take the lazy, slow, boring pseudo-synchronous way. It's ok... @@ -17,7 +19,38 @@ async function showTimes() { alert(time3); } +showTimes() + ``` No much fun. -There is a better way. Use the promise API +There is a better way. Make use of the promise API. + + +# Let's race! + +```js run + + +async function race() { + const results = await Promise.all([ + babieca.run(), + rocinante.run(), + bucephalus.run() + ]); + + alert("All the horses reached the goal! πŸŽ‰πŸ‡\n" + results.join('\n')); +} + +race(); + +``` + +This has no cost for your code. The horses run simultaneously. You may see when they are arriving in your console. + + +Please note: if you only care for the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones + + const fastest = await Promise.any([babieca.run(), rocinante.run(), bucephalus.run()]); + alert(`The winner: ${fastest}`); + diff --git a/1-js/11-async/08-async-await/04-still-a-promise/solution2.md b/1-js/11-async/08-async-await/04-still-a-promise/solution2.md deleted file mode 100644 index 5fdca165a7..0000000000 --- a/1-js/11-async/08-async-await/04-still-a-promise/solution2.md +++ /dev/null @@ -1,28 +0,0 @@ - -Let's race! - -```js run - - -async function race() { - const results = await Promise.all([ - babieca.run(), - rocinante.run(), - bucephalus.run() - ]); - - alert("All the horses reached the goal! πŸŽ‰πŸ‡\n" + results.join('\n')); -} - -race(); - -``` - -This has no cost for your code. The horses run simultaneously. You may see when they are arriving in your console. - - -Please note: if you only care for the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones - - const fastest = await Promise.any([babieca.run(), rocinante.run(), bucephalus.run()]); - alert(`The winner: ${fastest}`); - 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 index 115b9855f2..368d954f6d 100644 --- 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 @@ -1,7 +1,8 @@ + # Still a promise -Make the horses run then show their times +Make the 3 horses run then show their times ```js @@ -11,11 +12,11 @@ class Horse { } async run() { - const time = Math.floor(Math.random() * 3) + 1; + const time = Math.floor(Math.random() * 30) + 10; // - await new Promise(resolve => setTimeout(resolve, time * 1000)); + await new Promise(resolve => setTimeout(resolve, time * 1000 / 20)); // 20x. We don't want to wait realistic times, do we? - const result = `${time * 20} segundos para ${this.name}!!! `; + const result = `${time.toFixed(3)} seconds for ${this.name}!!! `; console.log(result); return result; } From 8ed6f9feb0bd3077335a8bad14e6fd22208f423c Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Tue, 28 Jan 2025 15:21:44 -0300 Subject: [PATCH 6/8] final lint --- .../04-still-a-promise/solution.md | 44 ++++++++++++++++--- .../08-async-await/04-still-a-promise/task.md | 7 ++- 2 files changed, 43 insertions(+), 8 deletions(-) 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 index e634025776..70f5cf5728 100644 --- 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 @@ -5,8 +5,10 @@ You may have been tempted to take the lazy, slow, boring pseudo-synchronous way. It's ok... -```js run +```js +// Your code +// async function showTimes() { const time1 = await babieca.run(); @@ -24,13 +26,35 @@ 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 30 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([ @@ -39,18 +63,26 @@ async function race() { bucephalus.run() ]); - alert("All the horses reached the goal! πŸŽ‰πŸ‡\n" + results.join('\n')); + 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 when they are arriving in your console. +This has no cost for your code. The horses run simultaneously. You may see as they are arriving in your console. + +If you only want the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones. +```js -Please note: if you only care for the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones + const fastest = await Promise.any([ + babieca.run(), + rocinante.run(), + bucephalus.run() + ]); - const fastest = await Promise.any([babieca.run(), rocinante.run(), bucephalus.run()]); - alert(`The winner: ${fastest}`); + alert(`The winner: ${fastest}`); + // Fun fact: slower horses will continue running inside the engine, but nobody cares anymore. +``` 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 index 368d954f6d..94daf8caec 100644 --- 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 @@ -12,11 +12,11 @@ class Horse { } async run() { - const time = Math.floor(Math.random() * 30) + 10; // + const time = Math.random() * 30 + 10; // 10 to 30 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(3)} seconds for ${this.name}!!! `; + const result = `${time.toFixed(2)} seconds for ${this.name}!!! `; // name, seconds.hundreths console.log(result); return result; } @@ -26,4 +26,7 @@ const babieca = new Horse('Babieca'); const rocinante = new Horse('Rocinante'); const bucephalus = new Horse('Bucephalus'); +// Your code... +// + ``` From f38b3a5584d2d844589512d31ef0180bbff0de0c Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Wed, 29 Jan 2025 23:04:26 -0300 Subject: [PATCH 7/8] split 2 tasks --- .../04-still-a-promise/solution.md | 14 ------- .../08-async-await/04-still-a-promise/task.md | 1 - .../05-there-can-be-only-one/solution.md | 41 +++++++++++++++++++ .../05-there-can-be-only-one/task.md | 31 ++++++++++++++ 4 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 1-js/11-async/08-async-await/05-there-can-be-only-one/solution.md create mode 100644 1-js/11-async/08-async-await/05-there-can-be-only-one/task.md 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 index 70f5cf5728..6ab3692f0b 100644 --- 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 @@ -72,17 +72,3 @@ race(); This has no cost for your code. The horses run simultaneously. You may see as they are arriving in your console. -If you only want the fastest horse, you may use `promise.any` so you dont even need to wait for the slower ones. - -```js - - const fastest = await Promise.any([ - babieca.run(), - rocinante.run(), - bucephalus.run() - ]); - - alert(`The winner: ${fastest}`); - // Fun fact: slower horses will continue running inside the engine, but nobody cares anymore. - -``` 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 index 94daf8caec..6d9da0e065 100644 --- 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 @@ -1,5 +1,4 @@ - # Still a promise Make the 3 horses run then show their times 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..0409cc353b --- /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 30 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..9abae699cd --- /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 30 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... +// + +``` From b23779b2d1bf42508ab5f1cb241ca558e4c386ca Mon Sep 17 00:00:00 2001 From: joaquinelio Date: Wed, 29 Jan 2025 23:48:30 -0300 Subject: [PATCH 8/8] oops --- 1-js/11-async/08-async-await/04-still-a-promise/solution.md | 2 +- 1-js/11-async/08-async-await/04-still-a-promise/task.md | 2 +- .../08-async-await/05-there-can-be-only-one/solution.md | 2 +- 1-js/11-async/08-async-await/05-there-can-be-only-one/task.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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 index 6ab3692f0b..3de50298a4 100644 --- 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 @@ -39,7 +39,7 @@ class Horse { } async run() { - const time = Math.random() * 30 + 10; // 10 to 30 seconds + 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? 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 index 6d9da0e065..e58202a1aa 100644 --- 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 @@ -11,7 +11,7 @@ class Horse { } async run() { - const time = Math.random() * 30 + 10; // 10 to 30 seconds + 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? 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 index 0409cc353b..3a0a65889b 100644 --- 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 @@ -7,7 +7,7 @@ class Horse { } async run() { - const time = Math.random() * 30 + 10; // 10 to 30 seconds + 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? 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 index 9abae699cd..aae4d3cb16 100644 --- 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 @@ -11,7 +11,7 @@ class Horse { } async run() { - const time = Math.random() * 30 + 10; // 10 to 30 seconds + 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?