Skip to content

preloadImages

Loads and decodes several images with decodeImage, a few at a time, with mapSettled from helpers4/array.

It never rejects: an image that fails does not stop the others, and its result says why.

Available since v3.1.5

import { preloadImages } from '@helpers4/browser';
// or, from the all-in-one package (same code, one install):
import { preloadImages } from 'helpers4/browser';
preloadImages(urls: readonly string[], options: PreloadImagesOptions): Promise<PromiseSettledResult<HTMLImageElement>[]>
ParameterTypeDescription
urlsreadonly string[]The image URLs
optionsPreloadImagesOptionsConcurrency — see PreloadImagesOptions

Promise<PromiseSettledResult<HTMLImageElement>[]> — One result per URL, in the same order

Each URL gets its own result, like Promise.allSettled: a broken image does not stop the others.

const results = await preloadImages(['/screens/a.png', '/screens/b.png']);
const failed = results.filter((result) => result.status === 'rejected');

concurrency caps how many images load at the same time (6 by default).

await preloadImages(thumbnails, { concurrency: 2 });

Options for preloadImages.

interface PreloadImagesOptions {
  concurrency?: number;
}

View source on GitHub