-
-
Save jussi-kalliokoski/7c5e476e056800f94493 to your computer and use it in GitHub Desktop.
Non-blocking sleep semantics for JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadImages (...images) { | |
var imagesToLoad = images.length; | |
var ready = return function; | |
function imageReady () { | |
if (!--imagesToLoad) { | |
ready(null, images); | |
} | |
} | |
function imageError (e) { | |
ready(e); | |
} | |
images = images.map(function (src) { | |
var img = new Image(); | |
img.src = src; | |
img.onload = imageReady; | |
img.onerror = imageError; | |
return img; | |
}); | |
} | |
try { | |
let [foo, bar, chuck] = loadImages('foo.gif', 'bar.png', 'chuck.jpg'); | |
console.log('All images loaded'); | |
} catch (e) { | |
console.error('There was an error loading an image:', e); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadImages (...images) { | |
var imagesToLoad = images.length; | |
var ready = return function; | |
function imageReady () { | |
if (!--imagesToLoad) { | |
ready(null, images); | |
} | |
} | |
function imageError (e) { | |
ready(e); | |
} | |
images = images.map(function (src) { | |
var img = new Image(); | |
img.src = src; | |
img.onload = imageReady; | |
img.onerror = imageError; | |
return img; | |
}); | |
finally yield ready; | |
} | |
try { | |
let [foo, bar, chuck] = yield loadImages('foo.gif', 'bar.png', 'chuck.jpg'); | |
console.log('All images loaded'); | |
} catch (e) { | |
console.error('There was an error loading an image:', e); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment