Ultima attività 1731093942

Async-1.js Raw
1// for use with callbacks...
2import { forEachOf } from "async-es";
3
4const images = {cat: "/cat.png", dog: "/dog.png", duck: "/duck.png"};
5const sizes = {};
6
7forEachOf(images, (value, key, callback) => {
8 const imageElem = new Image();
9 imageElem.src = value;
10 imageElem.addEventListener("load", () => {
11 sizes[key] = {
12 width: imageElem.naturalWidth,
13 height: imageElem.naturalHeight,
14 };
15 callback();
16 });
17 imageElem.addEventListener("error", (e) => {
18 callback(e);
19 });
20}, err => {
21 if (err) console.error(err.message);
22 // `sizes` is now a map of image sizes
23 doSomethingWith(sizes);
24});