Promise is not resolving even after calling resolve()

2.6k Views Asked by At

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?

2

There are 2 best solutions below

2
On

I have faced this type of error while making API calls or fetching data from the db. Generally the issue used to be type mismatch of called resources or header is having wrong return type. It would be good to have console.log in getById() and see what's happening thete in the stack trace.

0
On

first of all, add .catch() to get rejected value.

function asyncOpr = () => { 
    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"));
                    }
            }).catch(err => { /* handle error */ })
        });
  }

The reason why you may not be getting the resolved value would be how you are using this function. Correct implementation would be:

function foo() {
  asyncOpr().then( val => console.log(val))
            .catch( err => console.log(err))
}

Also, see using async/await.