May 2018
Intermediate to advanced
376 pages
9h 22m
English
[source,js]
----
async function fetchImage(url) {
const resp = await fetch(url);
const blob = await resp.blob();
return createImageBitmap(blob);
};
fetchImage('my-image.png').then(image => {
// do something with image
});
----
To achieve this without promises or async functions, you would need to write much more obtuse code.
[source,js]
----
function fetchImage(url, cb) {
fetch(url, function(resp) {
resp.blob(function(blob) {
createImageBitmap(blob, cb);
})
})
};
fetchImage('my-image.png', function(image) {
// do something with image
});
----