Skip to content
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

Catwalk async await #18

Open
wants to merge 2 commits into
base: main
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
50 changes: 34 additions & 16 deletions Week1/prep-exercises/1-catwalk-promises/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,56 @@
const STEP_SIZE_PX = 10;
const STEP_INTERVAL_MS = 50;
const DANCE_TIME_MS = 5000;
const DANCING_CAT_URL =
'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif';
const WALKING_CAT_URL = 'http://www.anniemation.com/clip_art/images/cat-walk.gif';
const DANCING_CAT_URL = 'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif';

function walk(img, startPos, stopPos) {
return new Promise((resolve) => {
// Resolve this promise when the cat (`img`) has walked from `startPos` to
// `stopPos`.
// Make good use of the `STEP_INTERVAL_PX` and `STEP_INTERVAL_MS`
// constants.
let currentPos = startPos;
img.style.left = `${currentPos}px`;
img.src = WALKING_CAT_URL;

const intervalId = setInterval(() => {
currentPos += STEP_SIZE_PX;
img.style.left = `${currentPos}px`;

if (currentPos >= stopPos) {
clearInterval(intervalId);
resolve();
}
}, STEP_INTERVAL_MS);
});
}

function dance(img) {
return new Promise((resolve) => {
// Switch the `.src` of the `img` from the walking cat to the dancing cat
// and, after a timeout, reset the `img` back to the walking cat. Then
// resolve the promise.
// Make good use of the `DANCING_CAT_URL` and `DANCE_TIME_MS` constants.
img.src = DANCING_CAT_URL;
setTimeout(() => {
img.src = WALKING_CAT_URL;
resolve();
}, DANCE_TIME_MS);
});
}

function catWalk() {
const img = document.querySelector('img');
if (!img) {
return;
}

const startPos = -img.width;
const centerPos = (window.innerWidth - img.width) / 2;
const stopPos = window.innerWidth;

// Use the `walk()` and `dance()` functions to let the cat do the following:
// 1. Walk from `startPos` to `centerPos`.
// 2. Then dance for 5 secs.
// 3. Then walk from `centerPos` to `stopPos`.
// 4. Repeat the first three steps indefinitely.

function performCatWalk() {
walk(img, startPos, centerPos)
.then(() => dance(img))
.then(() => walk(img, centerPos, stopPos))
.then(() => performCatWalk());
}

performCatWalk();
}

window.addEventListener('load', catWalk);
window.addEventListener('load', catWalk);
31 changes: 26 additions & 5 deletions Week2/prep-exercises/1-catwalk-async-await/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@
const STEP_INTERVAL_MS = 50;
const STEP_SIZE_PX = 10;
const DANCE_TIME_MS = 5000;
const WALKING_CAT_URL = 'http://www.anniemation.com/clip_art/images/cat-walk.gif';
const DANCING_CAT_URL =
'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif';


function walk(img, startPos, stopPos) {
return new Promise((resolve) => {
// Copy over the implementation from last week
let currentPos = startPos;
img.style.left = `${currentPos}px`;
img.src = WALKING_CAT_URL;

const intervalId = setInterval(() => {
currentPos += STEP_SIZE_PX;
img.style.left = `${currentPos}px`;

if (currentPos >= stopPos) {
clearInterval(intervalId);
resolve();
}
}, STEP_INTERVAL_MS);
});
}

function dance(img) {
return new Promise((resolve) => {
// Copy over the implementation from last week
img.src = DANCING_CAT_URL;
setTimeout(() => {
img.src = WALKING_CAT_URL;
resolve();
}, DANCE_TIME_MS);
});
}

Expand All @@ -23,8 +41,11 @@ async function catWalk() {
const startPos = -img.width;
const centerPos = (window.innerWidth - img.width) / 2;
const stopPos = window.innerWidth;

// Use async/await syntax to loop the walk and dance functions

while (true) {
await walk(img, startPos, centerPos);
await dance(img);
await walk(img, centerPos, stopPos);
}
}

window.addEventListener('load', catWalk);