O'Reilly logo

Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond by JD Isaacks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Here’s a preview of using promises and async functions from unit 7

[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
});
----

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required