Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6214

Resolve promise after callback it's executed

$
0
0

I have the following code on which I am trying to block the execution of the method _saveAddress multiple time, so I made a promise for this method.

  const [pressEventDisabled, setPressEventDisabled] = useState(false);<TouchableOpacity        style={style.button_container}        activeOpacity={1}        disabled={pressEventDisabled}        onPress={async () => {          setPressEventDisabled(true);          await _saveAddress();          setPressEventDisabled(false);        }}>

The problem is that I want to resolve the promise after the callback method it's executed. It's there any way to wait for the dispatch function to execute or to resolve the promise inside the callback method?

This is the method for saving the address:

    const _saveAddress = () =>        new Promise(async (resolve) => {          var valid = _validate();          if (valid) {            const address = createAddressJson();            if (addressId) {              var addressIdProperty = {id: addressId};              const newAddress = Object.assign(addressIdProperty, address);              dispatch(editAddress(newAddress, _onAddressSaveEditCallback));            } else {              dispatch(addAddress(address, _onAddressSaveEditCallback));            }          } else {            //notify            notifyMessage(strings.fill_required_inputs_validation);            resolve();          }        });

This is the callback method:

      const _onAddressSaveEditCallback = async (        success: boolean,        apiValidations: any,        address?: Address,      ) => {        if (success) {          if (typeof callback == 'function') {            callback(address);          }          await Navigation.pop(componentId);        } else {          setDataValidations(apiValidations);        }      };

Viewing all articles
Browse latest Browse all 6214

Trending Articles