My question here may sound little repetitive. But I haven't found any answer on stackoverflow which can solve my problem.
I am returning a promise from one of my function. The promise will be resolved after getting some data from database. And will be rejected if there is no data in the db table. Also I have to do some processing on the fetched data before resolving the promise, that's why I am using callbacks.
My Code Snippet
return new Promise((resolve, reject) => {
ResourceModel.getById(resource_ID).then((data) => {
if (data.length > 0) {
console.log(data);
resolve(data);
console.log("Resolve Function Called");
} else {
console.log("No data available");
reject(new Error("No data available"));
}
});
});
ResourceModel.getById()
static getById(id) {
const _transaction = cds.transaction();
return _transaction.run(SELECT.from(Resource).where({ ID: id }));
}
cds.transaction()
is one of cds framework provided method by SAP.
What I expect it to do is after successfully fetching all the records it should call the provided callback where I have my processing logic written and call resolve()
after the processing is done.
I am receiving the data in the callback, able to print Data available
and the received data object end even the Promise Resolved
on the console. But surprisingly the promise is not getting resolved
I can say the "promise is not getting resolved" because this returned promise will be collected in a Promise.all()
by the framework (as stated in the official docs - https://cap.cloud.sap/docs/node.js/api#service-before) and return a response after resolving. But I am not getting any response (neither success nor failure). Postman stays in loading... state forever.
The above example works fine with setTimeout()
example.
where am I going wrong?