I'm using loopback 3
to build a REST service, and I want to use the async/await instead of having to use the callbacks. So instead of doing that:
MyModel.myFunction = (callback) => {
MyModel.find({where: {id: 2}}, (e, data) => {
if (e) return callback(e);
callback(null, data);
});
};
I would greatly prefer to do:
MyModel.myFunction = async (callback) => {
try {
const data = await MyModel.find({where: {id: 2}});
callback(null, data);
} catch (e) {
console.error(e);
callback(e);
}
};
The callback approach works perfectly - async/await however gives numerous errors:
UnhandledPromiseRejectionWarning: Error: Callback was already called.
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
What's wrong? I cannot get through this issue.
callback
throws. – Occupantserver.js
, just tried it and it throws no errors. – Corkedmymodel.js
file (as the additional remote endpoint). – Porous