Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromjoaquinelio:promisetask
Open
Show file tree
Hide file tree
Changes fromall 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 deletions1-js/11-async/08-async-await/04-still-a-promise/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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...
//

```
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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...
//

```

[8]ページ先頭

©2009-2025 Movatter.jp