我怀疑如何同时执行两个功能。我想我应该使用Promise.All(),但是我不知道如何使用。
您好,代码结构如下:
scan1(){
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
return;
}
if (
(device.name == this.model_dx(this.props.Model)) ||
(device.name == this.model_sx(this.props.Model))
) {
this.setState({ deviceId1: device.id });
this.manager.stopDeviceScan();
this.setState({deviceName1: device})
console.log("(this.state.deviceName1 = device:): " + this.state.deviceName1);
device
.connect()
.then(() => {
console.log(" ");
this.scan2();
//..code...
scan2(){
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
return;
}
if (
(device.name == this.model_sx(this.props.Model)) ||
(device.name == this.model_dx(this.props.Model))
) {
this.manager.stopDeviceScan();
this.setState({ deviceId2: device.id });
this.setState({deviceName2: device})
device
.connect()
.then(() => {
this.deviceService1(this.state.deviceName1);
this.deviceService2(this.state.deviceName2);
// .... code....
deviceService1(device) {
console.log("device.name: " + device.name)
if (device.name == this.model_dx(this.props.Model) ||
device.name == this.model_sx(this.props.Model)) {
device
.discoverAllServicesAndCharacteristics()
.then(() => {
console.log("(this.setupNotifications1")
this.setupNotifications1(device);
})
//... code..
deviceService2(device) {
console.log("device.name: " + device.name)
if (device.name == this.model_sx(this.props.Model) ||
device.name == this.model_dx(this.props.Model)) {
device
.discoverAllServicesAndCharacteristics()
.then(() => {
console.log("(this.setupNotifications2")
this.setupNotifications2(device);
})
//... code...
在两个功能setupNotifications1(设备)和setupNotifications2(设备)中,我从外部设备恢复数据。这里的问题实际上是这样的:我先从一台设备恢复数据,然后再从另一台设备恢复数据,因此我从第一台设备中获取了更多数据。
现在我该如何一起执行这两个功能setupNotification?
谢谢!