Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit e7ab098

Browse files
committed
feat: Coding Challenge 3 of Asynchronous Section Done
1 parent 055f4c7 commit e7ab098

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

08-asynchronous/script.js

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,72 @@ const createImage = function (imgPath)
197197
});
198198
};
199199

200-
createImage('img/img-1.jpg')
201-
.then(() => wait(2))
202-
.then(() => hideImg(currImg))
203-
.then(() => createImage('img/img-2.jpg'))
204-
.then(() => wait(2))
205-
.then(() => hideImg(currImg))
206-
.then(() => createImage('img/img-error.jpg'))
207-
.catch(err => console.log(err));
200+
// createImage('img/img-1.jpg')
201+
// .then(() => wait(2))
202+
// .then(() => hideImg(currImg))
203+
// .then(() => createImage('img/img-2.jpg'))
204+
// .then(() => wait(2))
205+
// .then(() => hideImg(currImg))
206+
// .then(() => createImage('img/img-error.jpg'))
207+
// .catch(err => console.log(err));
208+
209+
// Coding Challenge 3
210+
211+
/*
212+
Your tasks:
213+
PART 1
214+
1. Write an async function 'loadNPause' that recreates Challenge #2, this time
215+
using async/await (only the part where the promise is consumed, reuse the
216+
'createImage' function from before)
217+
2. Compare the two versions, think about the big differences, and see which one
218+
you like more
219+
3. Don't forget to test the error handler, and to set the network speed to “Fast 3G”
220+
in the dev tools Network tab
221+
PART 2
222+
1. Create an async function 'loadAll' that receives an array of image paths
223+
'imgArr'
224+
2. Use .map to loop over the array, to load all the images with the
225+
'createImage' function (call the resulting array 'imgs')
226+
3. Check out the 'imgs' array in the console! Is it like you expected?
227+
4. Use a promise combinator function to actually get the images from the array 😉
228+
5. Add the 'parallel' class to all the images (it has some CSS styles)
229+
Test data Part 2: ['img/img-1.jpg', 'img/img-2.jpg', 'img/img-
230+
3.jpg']. To test, turn off the 'loadNPause' function
231+
*/
232+
233+
const loadNPause = async function (imgPath)
234+
{
235+
try
236+
{
237+
const img = await createImage(imgPath);
238+
await wait(2);
239+
hideImg(img);
240+
}
241+
catch (err)
242+
{
243+
console.error(err);
244+
}
245+
};
246+
247+
// loadNPause('img/img-1.jpg');
248+
249+
const loadAll = async function (...imgArr)
250+
{
251+
try
252+
{
253+
const imgs = imgArr.map(async img => await createImage(img));
254+
console.log(imgs);
255+
256+
const imgsEl = await Promise.all(imgs);
257+
console.log(imgsEl);
258+
259+
imgsEl.forEach(img => img.classList.add('parallel'));
260+
}
261+
catch (err)
262+
{
263+
console.error(err);
264+
}
265+
266+
};
267+
268+
loadAll('img/img-1.jpg', 'img/img-2.jpg', 'img/img-3.jpg');

0 commit comments

Comments
 (0)