Building on the ES7 async-await intro, here’s how to reach callbackless code.
Using async-await
// With xkcd-imgs module
const xkcd = require('xkcd-imgs');
async function getComic() {
const comic = await xkcd.random();
console.log(comic);
}
## With Fetch API
javascript
async function fetchData(url) {
const response = await fetch(url);
const data = await response.json();
return data;
}
Use Babel to transpile async-await for older environments.
Dive in and enjoy async programming in JavaScript without callbacks!
#javascript#async#es7
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.