如何在promise.all上处理挂起的promise

时间:2022-09-15 20:47:19

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

我有一个对象数组,并使用Promise.all将Api调用返回的值映射到每个对象的特定属性上

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:

API调用:

  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.

我遇到的问题是,有些情况下,Api会在某些调用中找不到。

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

在根据数组调用Api之后,我想继续并返回映射的对象,无论如何。

1 个解决方案

#1


1  

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

我的猜测是myApi.getItem拒绝承诺。你需要以某种方式捕获这种拒绝:

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
    }
  })
}

#1


1  

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

我的猜测是myApi.getItem拒绝承诺。你需要以某种方式捕获这种拒绝:

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
    }
  })
}

相关文章