-1

I have an array of objects and using Promise.all to map values returned from an Api call on a specific property on each object

The initial call looks like the following:

   Promise.all(jobs.map(job => convertItemsl(job)))
        .then( 
    doSomething()
        });
      })
  .catch(err)

function convertItemsl (job){
  return myApi.getItem(job.id).then( response => {
    const name = response.name ? response.name : ‘’;
    return {
        name: name,
        status: job.status
      };
    }
  )
}

API call:

  getItem(){
    return super.get('jobs').then(res => res.json());
  }

The problem I am experiencing is, there are expected cases where the Api will return not found on some calls.

After all calls to the Api as per array , I would like to continue and return the mapped objects regardless.

user1526912
  • 15,818
  • 14
  • 57
  • 92
  • Can you post the code for `myApi.getItem` including the promise library it uses? If it is "returning" not found then the promise in `convertItemsl` should be resolved either way. – Mitch Lillie Apr 27 '17 at 18:15
  • only some items are not found – user1526912 Apr 27 '17 at 18:22
  • Can you post the code for `myApi.getItem` including the promise library it uses? – Mitch Lillie Apr 27 '17 at 18:33
  • "*there are expected cases where the Api will return not found*" - what do you want to do in those cases? And what does the Api do instead, when it does not return found - does it return something else, or reject the promise? It should not stay pending. – Bergi Apr 27 '17 at 18:39
  • 2
    possible duplicate of [Wait until all ES6 promises complete, even rejected promises](http://stackoverflow.com/q/31424561/1048572) – Bergi Apr 27 '17 at 18:41

1 Answers1

1

My guess is myApi.getItem is rejecting its promise. You need to catch that rejection somehow:

function convertItemsl (job){
  return myApi.getItem(job.id).then( response => {
    const name = response.name ? response.name : ‘’;
    return {
        name: name,
        status: job.status
      };
    }
  ).catch(err => {
    return {
        name: null,
        // err.code or whatever your error looks like, maybe just 404
        status: err.code
    }
  })
}
Mitch Lillie
  • 2,217
  • 18
  • 25
  • I am was actually catching the error in the same place that you mentioned... just wasnt returning anything. I can understand why you would have to return something in this case – user1526912 Apr 27 '17 at 18:41