- Notifications
You must be signed in to change notification settings - Fork3.9k
Still a Promise#3808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
joaquinelio wants to merge8 commits intojavascript-tutorial:masterChoose a base branch fromjoaquinelio:promisetask
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Still a Promise#3808
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
5ef6ae1
Bosquejo inicial- probar- ojo necesita agregar codigo a sol1, 2
joaquinelio7d8b7f5
oops
joaquinelio37abe8c
Update solution.md
joaquinelio8d0d84d
Update solution2.md
joaquineliod7012ed
Two solutions one file
joaquinelio8ed6f9f
final lint
joaquineliof38b3a5
split 2 tasks
joaquineliob23779b
oops
joaquinelioFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions1-js/11-async/08-async-await/04-still-a-promise/solution.md
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
31 changes: 31 additions & 0 deletions1-js/11-async/08-async-await/04-still-a-promise/task.md
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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... | ||
// | ||
``` |
41 changes: 41 additions & 0 deletions1-js/11-async/08-async-await/05-there-can-be-only-one/solution.md
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
``` |
31 changes: 31 additions & 0 deletions1-js/11-async/08-async-await/05-there-can-be-only-one/task.md
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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... | ||
// | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.