I am using couchbase ottaman package:
let transaction = new Transaction({name:'Couch'});
transaction.save((err) => {
console.log(err);
});
but can I use async/await with this package?
async create(){
let transaction = new Transaction({name:'Couch'});
try{
await transaction.save();
} catch (err) {
console.log(err);
}
}
I am getting error:
node_modules\ottoman\lib\modelinstance.js:457
callback(err);
^
TypeError: callback is not a function
Basically, you have to check if it returns promise - you can use it out of box. If not - you can promisify function you need. Something like this in result:
function saveModel(transaction) {
return new Promise ((resolve, reject) => {
transaction.save(err => {
if (err)
reject(err);
else
resolve();
});
});
}
Such function can be used with async/await:
async create(){
let transaction = new Transaction({name:'Couch'});
try{
await saveModel(transaction);
} catch (err) {
console.log(err);
}
}
Another option is to view source files, but I'm too lazy to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With