Javascript React中未处理的拒绝承诺
我有一个从外部设备读取数据的应用程序。这些数据就像加速度、陀螺仪、磁强计和压力 我试图以这种方式读取数据:Javascript React中未处理的拒绝承诺,javascript,react-native,Javascript,React Native,我有一个从外部设备读取数据的应用程序。这些数据就像加速度、陀螺仪、磁强计和压力 我试图以这种方式读取数据: async setupNotifications2(device) { let i = 0 const service = this.serviceGeneral(); while(i<10 ) { i++ const promises = [ device.readCharacteristicForService(se
async setupNotifications2(device) {
let i = 0
const service = this.serviceGeneral();
while(i<10 ) {
i++
const promises = [
device.readCharacteristicForService(service, this.AccGyrMg),
device.readCharacteristicForService(service, this.Pressure)
]
Promise.all(promises)
.then((values) => {
// const time = new Date().getMilliseconds()
const time = bufAccGyrMg.readInt16LE(0);
const [...acc_sx] = [0,2, 4, 6].map(index => bufAccGyrMg.readInt16LE(index));
this.setState(state => ({acc_sx,array_acc_sx: [...state.array_acc_sx,[time , acc_sx]]}));
const [...gyr_sx] = [8, 10, 12].map(index => bufAccGyrMg.readInt16LE(index));
this.setState(state => ({gyr_sx,array_gyr_sx: [...state.array_gyr_sx,[time , gyr_sx]]}));
const [...mg_sx] = [14,16,18].map(index => bufAccGyrMg.readInt16LE(index));
this.setState(state => ({gyr_sx,array_mg_sx: [...state.array_mg_sx,[time , mg_sx]]}));
const bufPressure = Buffer.from(values[1].value, "base64");
const [...pressure_sx] = [0, 2, 4, 6, 8].map(index => bufPressure.readUInt16LE(index));
this.setState(state => ({pressure_sx,array_pressure_sx: [...state.array_pressure_sx,[time, pressure_sx]]}));
})
}
}
现在我在一段时间内插入了一个条件,只是为了尝试代码。
启动应用程序时,我收到以下错误:
YellowBox.js:67可能未处理的承诺拒绝id:0:
错误:操作被拒绝
在你看来,我该怎么办??多谢各位 我对您的代码进行了一些重构,以帮助您消除未处理的拒绝承诺错误,并帮助您指出问题所在:
async setupNotifications2(device) {
//first of all, put everything inside a big try/catch block
try {
let i = 0
const service = this.serviceGeneral();
while(i<10 ) {
i++
const promises = [
// then make sure every promise passed to Promise.all() catches its own errors
device.readCharacteristicForService(service, this.AccGyrMg).catch( e => console.log(`err in first readCharacteristicForService `, e)),
device.readCharacteristicForService(service, this.Pressure).catch( e => console.log(`err in second readCharacteristicForService `, e))
]
// giben you're in an async function, you can do this to simplify a bit your code:
const values = await Promise.all(promises);
// const time = new Date().getMilliseconds()
const time = bufAccGyrMg.readInt16LE(0);
// this is an array, no need to overcomplicate with destructuring assignment... you can do the same below
const acc_sx = [0,2, 4, 6].map(index => bufAccGyrMg.readInt16LE(index));
this.setState(state => ({acc_sx,array_acc_sx: [...state.array_acc_sx,[time , acc_sx]]}));
const [...gyr_sx] = [8, 10, 12].map(index => bufAccGyrMg.readInt16LE(index));
this.setState(state => ({gyr_sx,array_gyr_sx: [...state.array_gyr_sx,[time , gyr_sx]]}));
const [...mg_sx] = [14,16,18].map(index => bufAccGyrMg.readInt16LE(index));
this.setState(state => ({gyr_sx,array_mg_sx: [...state.array_mg_sx,[time , mg_sx]]}));
const bufPressure = Buffer.from(values[1].value, "base64");
const [...pressure_sx] = [0, 2, 4, 6, 8].map(index => bufPressure.readUInt16LE(index));
this.setState(state => ({pressure_sx,array_pressure_sx: [...state.array_pressure_sx,[time, pressure_sx]]}));
} catch (err){
console.error(`general error: `, err)
}
}
谢谢你的回答!我收到一个错误:error-TypeError:无法读取未定义的属性“value”你知道为什么吗?这是因为第二个promise设备。readCharacteristicForServiceservice,this。Pressure返回未定义,或者崩溃。你是对的,我首先收到这个错误,但我不明白为什么:第一个readCharacteristicForService BLERROR中的err1错误:操作被拒绝我不知道如何修复该错误,我认为最好为此创建另一个问题,因为它似乎是特定于设备的。readCharacteristicForService。。。方法是我假设的一些androidsdk,而不是javascript。另外,请接受我的正确答案,因为它有助于你确定实际问题。