Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions 1-js/11-async/08-async-await/04-still-a-promise/solution.md
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 deletions 1-js/11-async/08-async-await/04-still-a-promise/task.md
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 deletions 1-js/11-async/08-async-await/05-there-can-be-only-one/solution.md
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 deletions 1-js/11-async/08-async-await/05-there-can-be-only-one/task.md
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...
//

```