Use Promises, not callbacks
Callbacks aren’t clean, and they cause excessive amounts of nesting. With ES2015/ES6, Promises are a built-in global type. Use them!
Bad:
import { get } from "request";
import { writeFile } from "fs";
get(
"https://en.wikipedia.org/wiki/Robert_Cecil_Martin",
(requestErr, response) => {
if (requestErr) {
console.error(requestErr);
} else {
writeFile("article.html", response.body, writeErr => {
if (writeErr) {
console.error(writeErr);
} else {
console.log("File written");
}
});
}
}
);
Good:
import { get } from "request";
import { writeFile } from "fs";
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);
});