Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
image-promise
Advanced tools
Load an image and return a promise in the browser, in 0.3KB, no dependencies
It can be used in two ways:
given a URL, generate an <img>
and wait for it to load:
loadImage('img.jpg').then(/*it's loaded!*/)
given an <img>
, wait for it to load:
const img = document.querySelector('img.my-image');
loadImage(img).then(/*it's loaded!*/)
Pick your favorite:
<script src="dist/image-promise.min.js"></script>
npm install --save image-promise
var loadImage = require('image-promise');
import loadImage from 'image-promise';
loadImage( '/cat.jpg' ); // one string
loadImage( document.querySelector('img') ); // one element
// Returns a Promise that resolves with an image (`<img>`)
loadImage( ['/cat.jpg', '/dog.png'] ); // Array of strings
loadImage( document.querySelectorAll('img') ); // any Array-like list of elements
// Returns a Promise that resolves with **an array of images.**
The promises resolve when the provided <img>
s are loaded.
Load one image:
loadImage('cat.jpg')
.then(function (img) {
ctx.drawImage(img, 0, 0, 10, 10);
})
.catch(function () {
console.error('Image failed to load :(');
});
Load multiple images
loadImage(['/cat.jpg', '/dog.png'])
.then(function (allImgs) {
console.log(allImgs.length, 'images loaded!', allImgs);
})
.catch(function (firstImageThatFailed) {
// it fails fast like Promise.all
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#Promise.all_fail-fast_behaviour
console.error('One or more images have failed to load :(');
});
None! But you need to polyfill window.Promise
in IE11 and lower.
MIT © Federico Brigante
FAQs
Load one or more images, return a promise. Only 0.4KB, for the browser, no dependencies.
The npm package image-promise receives a total of 2,742 weekly downloads. As such, image-promise popularity was classified as popular.
We found that image-promise demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.