Search code examples
javascriptreactjspromisees6-promise

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


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?

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?
    

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?


Solution

  • In your example i think you received a warning. But still how to access to activeCustomers it depends on how you want to use it there are you storing it.

    If you want to store it globally then you can store it like that

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

    But i think it's better to rewrite to 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;
    };
    

    And you can use it like const customers = await CreateCustomer(someData); or like CreateCustomer(someData).then((cusomters) => { activeCustomers = cusomters; return null;(if it keeps to return errors)});