JSFiddle - React, Tailwind, and code Playground

by Deepak Anand

JavaScript

// Understanding Deferred and Promise using Image loading as an example

// Function to load images that takes a data-uri, it returns a promise.

// uri is base 64 encoded form of the image
var load = function(uri){
 dfd = new Deferred(), img = new Image()
 img.onload = function(){
  img.onload = img.onerror = null;
  dfd.resolve(img);
 }
 img.setAttribute("src", uri);
 return dfd.promise;
}
}


// usage
var data uri = 'sddsfdsf'; //TODO
var p = load(datauri);
p.then(function(){
 console.log(img)
})