通过 map 在 运行 之后从 React JS 中的 promise 获取变量 data/result

Get the variable data/result from the promise in React JS after running it through map

我想要最后一个 then 中的 activeCustomers 数组的结果,但我一直收到错误消息说箭头函数需要一个 return。不确定如何获得 activeCustomers?

const CreateCustomer = (storeData) => {
  let activeOrganization = null;
  storeData.Org
    .getOrganization()
    .then(function createCustomer(organization) {
      activeOrganization = organization[0];

      const dataArray= storeData.attributes;
      activeOrganization 
        .createAttributes(
          attributeType[0],
          getSomeData(dimensions)
        )
        .then(function Properties(createdAttribute) {
          updateCustomerProperty(createdAttribute, attributeType[0]);
        });
       activeOrganization 
        .createAttributes(
          attributeType[1],
          getSomeData(dimensions)
        )
        .then(function Properties(createdAttribute) {
          updateCustomerProperty(createdAttribute, attributeType[1]);
        });
     }).then(() => {
      activeOrganization
        .getCustomers()
        .then((cusomters) => {
          const activeCustomers = [];
          cusomters.map((customer) => {
            activeCustomers.push(customer);
          });
          return activeCustomers;
        })
        .then((activeCustomers) => {
          console.log(activeCustomers);
        });
    });
};

//Now I want the result of activeCustomers array inside the last then but I keep getting an error saying arrow function expects a return. Not sure how I can get activeCustomers?
    

我想要最后一个 then 中的 activeCustomers 数组的结果,但我一直收到错误消息说箭头函数需要一个 return。不确定如何获得 activeCustomers?

在您的示例中,我认为您收到了警告。但是仍然如何访问 activeCustomers 取决于你想如何使用它,你存储它。

如果你想全局存储它,那么你可以这样存储它

let activeCustomers;
....
.then((cusomters) => {
          activeCustomers = [];
          cusomters.map((customer) => {
            activeCustomers.push(customer);
          });
          return activeCustomers;
        })

不过我觉得改成async/await比较好。

const CreateCustomer = async (storeData) => {
  let activeOrganization = null;
  const [activeOrganization] = await storeData.Org
    .getOrganization();

  const activeOrgPrs = attributeType.map(x => activeOrganization 
        .createAttributes(
          x,
          getSomeData(dimensions)
        )));

  const attrs = await Promise.all(activeOrgPrs);

  attrs.forEach((attr, i) => {
      updateCustomerProperty(attr, attributeType[i]);
   })
  
   const cusomters = await activeOrganization
        .getCustomers();
   return cusomters;
};

你可以像const customers = await CreateCustomer(someData);一样使用它 或者喜欢 CreateCustomer(someData).then((cusomters) => { activeCustomers = cusomters; return null;(if it keeps to return errors)});