See https://github.com/therootcompany/async-router
try {
let results;
try {
results = await ProfileModel.get(req.user.id);
} catch (err) {
if ("E_NO_RECORD" !== err.code) {
throw err;
}
results = Profile.create();
}
res.json(await doStuff(results));
} catch (e) {
next(err);
}
return ProfileModel.get(req.user.id)
.catch(function (err) {
if ("E_NO_RECORD" !== err.code) {
throw err;
}
return Profile.create();
})
.then(function (results) {
return doStuff(results).then(res.json);
})
.catch(next);
let results = await ProfileModel.get(req.user.id)
.catch(function (err) {
if ("E_NO_RECORD" !== err.code) {
throw err;
}
return Profile.create();
});
res.json(await doStuff(results));
Assertions:
😎 Clean Code reads top to bottom ⬇️
Ugly Code reads left to right ➡️
🥴 Atrocious Code reads left to right ➡️ to bottom to top ⬆️
try / catch is a code smell 👃
👹 try / await is code hell
await / catch is elegant 😇