0

I have following function:

let exists;

function getCurrentRfqBusinesspartnerAndCheckIfIdExists(Id) {
    $http({
        method: 'POST',
        url: "https://test.com/anyapi",
        data: {
            Value: apiId
        }

    }).then(function (response) {
        $scope.Context.currentRfqBusinesspartner = response.data;
        currentRfqBusinesspartner = response.data;

        response.data.BusinessPartner.forEach(function (bp) {
            if (bp.Id == Id) {
                exists = true;
            }
        })
    });
    return exists;
}

If I check the value of the function in further code, I always get the result 'unavailable'. I think the problem is due to the asynchronous running of the code. Is there any solution to solve the issue?

Maybe there is any possibility to solve the problem by promise or callback?

Simon
  • 21
  • 2
  • Return the promise you already have. Return the computed value from the `then` function so the promise resolves as that. Don't reinvent [Array.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find). – Quentin Nov 03 '22 at 14:17

0 Answers0