1

I'm trying to chain two http calls. The first one returns a set of records and then I need to get finance data for each of them.

flightRecordService.query().$promise.then(function (flightRecords) {
  $scope.flightRecords = flightRecords;
  for (var i = 0; i < $scope.flightRecords.length; i++) {
    $scope.flightRecords[i].financeDocument =
      financeDocumentService
      .isReferencedDocumentIdCompensated({
        id: $scope.flightRecords[i].id
      }).$promise.then(
        function (data) {
          return ({
            'isCompensated': data.headers['compensated']
          });

        }
      );
    console.log($scope.flightRecords);
  }
});

This is the FlightRecord object:

$$hashKey: "object:27"
aircraft: {id: 100, registration: "LV-OEE", model: "152", status: "ACTIVE", brand: "Cessna", …}
amountOfHours: 1
canceled: false
closed: false
crew: [Object] (1)
destiny: null
endFlight: "2017-01-06T20:54:05.296"
financeDocument: d
  --> $$state: {status: 1, value: {isCompensated: "false"}}
  --> d prototipo
id: 100
landings: 0
nature: "LDI"
opened: true
origin: null
purpose: "VP"
startFlight: "2017-01-06T19:44:05.296"
status: "OPENED"
type: "ENT"

financeDocument object has not the structure I expect... I need a the following format:

...
endFlight: "2017-01-06T20:54:05.296"
financeDocument: { isCompensated: "false" }
id: 100
...

What I need to change to get that?

Thanks a lot!!

3 Answers 3

2

What you'll want to do is modify each "flight record" entry when you've retrieved the extra details. You'll also probably want to use $q.all to signal to the caller that the operation is complete.

const promise = flightRecordService.query().$promise.then(flightRecords => {
  return $q.all(flightRecords.map(flightRecord => {
    return financeDocumentService.isReferencedDocumentIdCompensated({
      id: flightRecord.id
    }).$promise.then(data => Object.assign(flightRecord, {
      isCompensated: data.headers.compensated
    }))
  }))
})

promise.then(flightRecords => {
  $scope.flightRecords = flightRecords
})
Sign up to request clarification or add additional context in comments.

4 Comments

Curious why you use Object.assign instead of just doing flightRecord.isCompensated = data.headers.compensated?
@FrankerZ Object.assign returns the amended object, thus resolving the promise with that object. The alternative would be data => {flightRecord.isCompensated = data.headers.compensated; return flightRecord}
I like that it returns a promise that can be used for more chaining
Thanks @Phil! I use solution provided by @FrankerZ
1

Why not just set it on the original object?

flightRecordService.query().$promise.then(function (flightRecords) {
  $scope.flightRecords = flightRecords;
  for (var i = 0; i < $scope.flightRecords.length; i++) {
      (function(record) {
          financeDocumentService
          .isReferencedDocumentIdCompensated({
            id: $scope.flightRecords[record].id
          }).$promise.then(
            function (data) {
              $scope.flightRecords[record].financeDocument = {
               'isCompensated': data.headers['compensated']
              }
            });
    })(i)
    console.log($scope.flightRecords);
  }
});

2 Comments

Hi @FrankerZ, first of all I want to thank you for your help then I want to ask you if you can help me I just want to understand what are you doing when you did: (function(record) { ..... })(i) Thanks a lot again!
It creates a capture and preserves the value of I (Which is record) for the duration of the function. Without it, the data function will overwrite the last key over and over again because i is now the last value.
-1

You are trying to set the financeDocument property synchronously using a Promise. You need to set the variable in the success callback of the promise.

1 Comment

Hi @amahfouz, how can I do this? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.