React Native promise.all呈现两次并返回undefined [duplicate]

sr4lhrrt  于 2023-01-27  发布在  React
关注(0)|答案(1)|浏览(143)
    • 此问题在此处已有答案**:

Promise.all is returning an array of undefined and resolves before being done(3个答案)
Why is my React component is rendering twice?(8个答案)
When should I use a return statement in ES6 arrow functions(6个答案)
2天前关闭。
大家好,我正在与一个React原生项目,我正在与离线工作,所以我必须离线存储数据,当互联网回来了,我必须重定向用户到屏幕,并发送数据到Web和更改百分比,但我的问题是,获取渲染两次,所有promis后返回的数据是未定义的,获取到服务器传递的方式在服务器和数据库是变化很好
下面是我的函数

export const syncOfflinedata = activities => {
  return dispatch => {
    Promise.all(
      activities.map(item => {
        Api()
          .put(putEvaluation(item._id), {evaluation: item.evaluation})
          .then(data => {
            dispatch(setProgressporcentage(1 / activities.length));
          })
          .catch(err => {
            console.log(err.message);
          });
      }),
    )
      .then(datafinished => {
        console.log('finished', datafinished);
      })
      .catch(e => {
        console.log(e);
      });
  };
};
0aydgbwb

0aydgbwb1#

map方法应该有return,除非它是用一行简短的形式写的:

export const syncOfflinedata = activities => {
  return dispatch => {
    Promise.all(
      activities.map(item => {
        return Api()
          .put(putEvaluation(item._id), {evaluation: item.evaluation})
          .then(data => {
            dispatch(setProgressporcentage(1 / activities.length));
          })
          .catch(err => {
            console.log(err.message);
          });
      }),
    )
    .then(datafinished => {
      console.log('finished', datafinished);
    })
    .catch(e => {
      console.log(e);
    });
  };
};

相关问题