How to call a service several times with different data

Viewed 22

I have a method that calls several times with different data when the app is loaded This is what I did:

useEffect(() => {
    if (ContinentId !== null) getRegionsByParentId(null ,regionType.Continent);
    if (ContinentId !== null) getRegionsByParentId(ContinentId ,regionType.Country);
    if (CountryId !== null) getRegionsByParentId(CountryId,regionType.Province);
    if (ProvinceId !== null) getRegionsByParentId(ProvinceId ,regionType.City);
    if (CityId !== null) getRegionsByParentId(CityId ,regionType.Section);
    if (props.data && Object.keys(props.data).length) {
      reset({ ...props.data });  
    }
  }, [props.data]);

and getRegionsByParentId method is:

 const getRegionsByParentId = (data ,regionSelectedType) => {
    var command = {
      entity: data,
    };
   Service.getRegionsByParentId(command, (status, data) => {
      if (data ) {
        switch (regionSelectedType.EnTitle) {
          case regionType.Continent.EnTitle:
            setContinentOptions(data.Result);
            return;
          case regionType.Country.EnTitle:
            setCountryData(data.Result);
            return;
            case regionType.Province.EnTitle:

            setProvinceData(data.Result);
            return;
            case regionType.City.EnTitle:
            setCityData(data.Result);
            return;
            case regionType.Section.EnTitle:
            setSectionData(data.Result);
            return;
        }
      }
    });
  };

But it only takes 2 of the fields and the rest get in trouble... Someone knows how to do this?

Can I use async await if yes how?

0 Answers
Related