Reputation: 1396
I have a question:
I have 4 functions that I wrote in the componentDidMount(), but I notice that the order in which I write these functions is not respected.
componentDidMount() {
this.checkPermission(); (1)
this.checkInitialBluetoothState(); (2)
this.disconnect() (3)
this.scans() (4)
}
I receive the console log in order from; (3) (2) (1) and (4) and sometimes the (4) function is not performed and the app enters in a loop
Do you recommend linking one function to another by calling it?
Thank you
EDIT:
async checkPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
{
title: 'Accesso Localizzazione',
message: 'Richiesto accesso localizzazione',
buttonNegative: 'Cancel',
buttonPositive: 'Ok',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Accesso Localizzazione Permesso.")
}
else {
console.log("Accesso Localizzazione Negato.")
}
} catch (err) {
console.warn(err)
}
}
async checkInitialBluetoothState() {
const isEnabled = await BluetoothStatus.state();
console.log("Controllo bluetooth on o off", isEnabled);
if (isEnabled == true) {
console.log("Bluetooth attivo.")
}
else {
Alert.alert(
'Attenzione:',
'Bluetooth non è attivo.'
);
Actions.homepageutente();
}
}
disconnectDevice() {
if (this.state.device1) {
this.manager.cancelDeviceConnection(this.state.device1.id)
}
else {
console.log("Device1 non connesso")
}
if (this.state.device2) {
this.manager.cancelDeviceConnection(this.state.device2)
}
else {
console.log("Device2 non connesso")
}
}
scans() {
//....
Upvotes: 0
Views: 165
Reputation: 67296
If you need async functions to execute in order. Do something like this:
async componentDidMount() {
await this.checkPermission(); (1)
await this.checkInitialBluetoothState(); (2)
this.disconnect() (3)
this.scans() (4)
}
Otherwise, the async
functions will return immediately.
Upvotes: 4