Async/Await are even cleaner than Promises
Promises are a very clean alternative to callbacks, but ES2017/ES8 brings async and await
which offer an even cleaner solution. All you need is a function that is prefixed
in an async
keyword, and then you can write your logic imperatively without
a then
chain of functions. Use this if you can take advantage of ES2017/ES8 features
today!
Bad:
import { get } from "request-promise";
import { writeFile } from "fs-promise";
get("https://en.wikipedia.org/wiki/Robert_Cecil_Martin")
.then(response => {
return writeFile("article.html", response);
})
.then(() => {
console.log("File written");
})
.catch(err => {
console.error(err);
});
Good:
import { get } from "request-promise";
import { writeFile } from "fs-promise";
async function getCleanCodeArticle() {
try {
const response = await get(
"https://en.wikipedia.org/wiki/Robert_Cecil_Martin"
);
await writeFile("article.html", response);
console.log("File written");
} catch (err) {
console.error(err);
}
}