Javascript wait实际上并不是在等待返回值
我正在编写一个使用蓝牙模块的react本机应用程序。我的目标是使scan/connect/discover函数异步,以便我可以等待它并相应地管理返回值 这是我现在掌握的代码:Javascript wait实际上并不是在等待返回值,javascript,asynchronous,Javascript,Asynchronous,我正在编写一个使用蓝牙模块的react本机应用程序。我的目标是使scan/connect/discover函数异步,以便我可以等待它并相应地管理返回值 这是我现在掌握的代码: async writeData(data) { this.store.dispatch({type: "START_LOADER"}) await this.manager.isDeviceConnected(this.deviceId).then(res => { if (res)
async writeData(data) {
this.store.dispatch({type: "START_LOADER"})
await this.manager.isDeviceConnected(this.deviceId).then(res => {
if (res) {
const device = this.store.getState().store.device
device.writeCharacteristicWithoutResponseForService(
data.serviceId,
data.charId,
data.dataToWrite
).then(res => {
this.store.dispatch({type: "SET_STATUS", payload: `${data.message}!\n`})
this.store.dispatch({type: "STOP_LOADER"})
}).catch(error => {
this.store.dispatch({type: "SET_ERROR", payload: error.message})
this.store.dispatch({type: "STOP_LOADER"})
})
return true
} else {
const timeout = setTimeout(() => {
this.store.dispatch({type: "STOP_LOADER"})
this.store.dispatch({type: "SET_ERROR", payload: 'Function timeout: device not found'})
return
}, 10000)
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
this.store.dispatch({type: "SET_ERROR", payload: error.message})
return
}
if (device.id === this.deviceId) {
clearTimeout(timeout)
this.store.dispatch({type: "SET_STATUS", payload: "North Sense found, stopping device scan...\n"})
this.store.dispatch({type: "SET_DEVICE", payload: device})
this.manager.stopDeviceScan();
device.connect().then((device) => {
this.store.dispatch({type: "SET_STATUS", payload: "device is connected!\n"})
return device.discoverAllServicesAndCharacteristics()
}).then((device) => {
device.writeCharacteristicWithoutResponseForService(
data.serviceId,
data.charId,
data.dataToWrite
).then(res => {
this.store.dispatch({type: "SET_STATUS", payload: `${data.message}!\n`})
this.store.dispatch({type: "STOP_LOADER"})
}).catch(error => {
this.store.dispatch({type: "SET_ERROR", payload: error.message})
this.store.dispatch({type: "STOP_LOADER"})
})
}).catch((error) => {
this.store.dispatch({type: "SET_ERROR", payload: error.message})
this.store.dispatch({type: "STOP_LOADER"})
});
}
});
}
}).catch(error => {
this.store.dispatch({type: "SET_ERROR", payload: error.message})
return error
})
}
此代码插入到类的上下文中,因此我使用此函数的方式如下:
const blApi = new blApi(deviceId)
console.log('1')
blApi.writeData({
serviceId: Config.SERVICE_ID,
charId: Config.ADD_PROGRAM_CHAR_ID,
dataToWrite: btoa(`4;${this.state.bearing};${Config.SENSING_LOCATION_INDEX}`),
message: "Person has been successfully added.",
})
console.log('2')
日志不会等待函数返回,特别是当触发超时(如果蓝牙设备不存在)并且超时在10秒后过期时,我希望函数在继续下一行之前等待这10秒
我如何解决这个问题?您的
等待不是很有用。应该等待wait
操作数完成的是什么?目前,这算不了什么;函数将在await
表达式完成后结束。你的意思是像return-await…
?看起来你把“传统”承诺代码和async/await
混在一起了。这没有错,但使用一种或另一种方法会更干净。试着阅读本文。也许你会理解得更好。通常async/await和then/catch是分开使用的。它们不是互补的,考虑使用一个.@ iMIUS,而我,我认为有时甚至在<代码> ASYNC/<代码> />代码>等待< /COD>代码。