I'm getting my hands dirty in Koa.js and am looking for a best practice on returned error handling for generators, if there is one. Take the following:
var sql = require('./lib/sql');
app.use(function *(){
var results = yield sql.query('select top 1 * from farm_animals;');
this.body = results;
});
Now, a tradition approach I would be used to would be:
sql.query('select top 1 * from farm_animals;', function(err, data){
if (!err) {
// return data
}
});
Now that I've converted sql.query
into a Promise
, I can't return err
and data
. So what should I return that could properly inform me of errors while returning the data? Perhaps I could adopt and stick to some standard:
{
error: null,
data: { id: 1, animal: 'cow' },
}
Before I do this however, I want to make sure there isn't some accepted best practice that I am missing.