Node.js 7.6 Released
nodejs.org
nodejs.org
async function logFetch(url) {
try {
const response = await fetch(url);
console.log(await response.text());
} catch (err) {
console.log('fetch failed', err);
}
} router.get('/users/:id', async (ctx, next) => {
const user = await db.getUser(ctx.params.id)
if (!user) return ctx.send(404)
let data
if (user.admin) data = await api.getSomeData(user)
return ctx.render('show_user.html', { user, data })
})Errors bubble up to the top-level. It's the old callback-style that swallows errors unless you manually handle them every time. It's one of the chief advantages of promises.
In my case, errors bubble up to error-catching middleware and, at the end of the chain, into a 500 error that gets logged.
So, there's quite a real risk of quiet failures in that bit of code.
Upd. Looks like even though the unhandled rejections warnings are getting deprecated, the rejections themselves will actually stop the code execution with non-zero exit code! Which means most of the issues I had with that code are gone (which is awesome)
Not sure why that is a "real risk". You generally want to be using promises with libraries that support them. That's why I wouldn't use promises in vanilla Express as my daily driver abstraction.
Compare this to callbacks where there's an actual real risk of silent error dropping any time you forget to `next(err)` or `.catch(handleErr)`, which is something you have to carry around for the life of the project instead of my example, where you choose a framework (once) that supports them.
I'd say it's a great demo and addressed all your concerns so far since it's this kind of context where async/await really pays off compared to the logFetch example.
Looks like this is the release they've been waiting for.
Looks like it might be a good time to move code from v4 (that had all those swallowing issues) to v7 and just forget about that. Promises sure came a long way :D
It's been coming for a while, but finally getting browsers that don't need nearly as much fluffy goodness from babel, which I'm happy to see.
Can someone explain this to me? Does this just mean I can open files using the 'file:' syntax or can I somehow open URLs with readFile now?