您好,我必须进行多个 Promise 调用,但我陷入困境,基本上我从通过 modbus/TCP 通信的套接字服务获取数据。我能够通过 cordova 插件进行通信,但我在掌握 Promise 的工作原理时遇到一些问题,这里有一些代码:
ngOnInit() {
console.log('init')
this.getRegistrationNumber().then(() => {
this.swVersionRequest()
})
}
getRegistrationNumber() {
let self = this
return new Promise(function(resolve, reject) {
self.socketService.getMatricola().then((data) => {
self.registrationNumber = self.socketService.arrayBuffer2str(data.data.slice(3, data.data.length-2))
console.log(`Registration number is ${self.registrationNumber}`)
resolve()
})
.catch(error => {
reject(error)
})
})
}
swVersionRequest() {
console.log("software version request");
this.socketService.getSwVersion().then((data) => {
this.softwareVersionWifiGsm = this.socketService.arrayBuffer2str(data.data.slice(3, data.data.length-2))
this.softwareVersionWifiGsm = this.softwareVersionWifiGsm.substring(0, 2).split('').join(".") + ' ' + this.transformSwVersion(this.softwareVersionWifiGsm.substring(2, this.softwareVersionWifiGsm.length))
console.log(`Software version is ${this.softwareVersionWifiGsm}`)
})
}
getMachineType() {
console.log('get machine type')
this.socketService.getMachineType().then((data) => {
this.machineType = data.data.slice(4, data.data.length-2)
console.log(`Machine type is ${this.machineType}`)
})
}
refreshData() {
this.getRegistrationNumber().then(() => {
this.swVersionRequest()
})
.then(() => { // not working like this
this.getMachineType()
})
}
transformSwVersion(str) {
return str.replace(/(\d\d)(\d\d)(\d\d)/, "$1/$2/$3")
}
然后,当我获得 machineType 时,我想检查 Promise 的响应,如下所示:
if (this.machineType == 2) {
callSoftwareVersionB() // another promise
} else {
callSoftwareVersionC() // another promise
}
不知道说清楚了没有
提前谢谢
最佳答案
我不明白你的所有问题,但你的问题似乎只与 promise 有关。
如果你想链接你的 Promise,你必须返回解析*(在这里包含你的数据*),如下所示:
return new Promise(function(resolve, reject) {
return self.socketService.getMatricola().then((data) => {
self.registrationNumber = self.socketService.arrayBuffer2str(data.data.slice(3, data.data.length-2))
console.log(`Registration number is ${self.registrationNumber}`)
return resolve(withYourDataHere)
})
之后,您将能够链接您的 Promise 并得到如下结果:
getRegistrationNumber().then((yourData) => {
console.log(yourData);
return yourData;
});
如果不需要之前的响应来执行下一个请求,则可以执行 Promise.all()。
例如,您可以这样做:
const promises = [];
promises.push(getRegistrationNumber());
promises.push(getMachineType());
//etc...
return Promise.all(promises).then(() => {
//your code will be executed after all your requests are resolved
})
就您而言:
refreshData() {
this.getRegistrationNumber().then(() => {
this.swVersionRequest()
})
.then(() => { // not working like this
this.getMachineType()
})
}
此代码变为:
swVersionRequest() {
console.log("software version request");
return this.socketService.getSwVersion().then((data) => {
this.softwareVersionWifiGsm = this.socketService.arrayBuffer2str(data.data.slice(3, data.data.length-2))
this.softwareVersionWifiGsm = this.softwareVersionWifiGsm.substring(0, 2).split('').join(".") + ' ' + this.transformSwVersion(this.softwareVersionWifiGsm.substring(2, this.softwareVersionWifiGsm.length))
console.log(`Software version is ${this.softwareVersionWifiGsm}`)
})
}
getMachineType() {
console.log('get machine type')
return this.socketService.getMachineType().then((data) => {
console.log(`Machine type is ${this.machineType}`)
return this.machineType = data.data.slice(4, data.data.length-2)
})
}
refreshData() {
return this.getRegistrationNumber().then(() => {
return this.swVersionRequest().then(() => {
return this.getMachineType() //you can chain that if you change the return in your function
})
})
}
或者
refreshData() {
const promises = [];
promises.push(this.getRegistrationNumber());
promises.push(this.swVersionRequest());
promises.push(this.getMachineType());
return Promise.all(promises).then(() => {
//type your code here
})
}
我同意 saglamcem 的观点,您在 MDN 上拥有执行此操作所需的所有文档,并且您也可以使用 Observables。
希望我们能帮助您。
关于javascript - 如何进行多个 Promise 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59879809/