Example 2-1. Using callbacks


loadImage('shadowfacts.png',
	function onsuccess(img) {
		console.log('received image');
	},
	function onerror(e) {
		console.log('Error occurred while loading image');
		console.log(e);
	}
);

function loadImage(url, success, error) {
	var img = new Image();
	img.src = url;

	img.onload = function () {
		success(img);
	};

	img.onerror = function (e) {
		error(e);
	};
}