gpt4 book ai didi

javascript - 无法在 Ionic 3 中链式 promise

转载 作者:行者123 更新时间:2023-12-03 02:20:09 25 4
gpt4 key购买 nike

我有一个插件,cordova-plugin-datecs-printer ,我可以用它在热敏打印机上打印一些字符串。问题是我无法以这种方式编码打印大量字符串。插件创建者已经通知我,错误在于我处理 promise 的方式,但我已经尝试以不同的方式改变我处理它们的方式,但我没有得到它。我尝试寻找一些示例并了解有关 Promise 的更多信息,但我无法将任何内容应用于此案例。

我的代码:

 printItem(){
this.print.listBluetoothDevices().then(result => {
console.log(JSON.stringify(result));
this.print = result;
}).catch(err => {

});

this.print.connect('00:02:5B:B4:7C:3A').then(result => {
console.log(JSON.stringify(result));
this.print = result;
}).catch(err => {

});

var printStr = "";
printStr += "{reset}{center}Instituto do Meio Ambiente {br}";
printStr += "------------------------------------------------";
printStr += "AUTO DE INFRACAO AMBIENTAL 9453-D {br}";
printStr += "------------------------------------------------";
printStr += "DADOS DO AUTUADO {br}";

var printStr2 = "";
printStr2 += " {br}";
printStr2 += "{reset}{left}Nome: ";
printStr2 += "ADD VARIAVEL NOME {br}";
printStr2 += "CPF: ";
printStr2 += "ADD VARIAVEL CPF {br}";

//Promises I want to chain
this.print.printText(printStr, 'ISO-8859-1');
this.print.printText(printStr2, 'ISO-8859-1');

this.print.feedPaper(100).then(result => {
console.log(JSON.stringify(result));
this.print = result;
}).catch(err => {

});

编辑:我正在使用的提供商(打印):

listBluetoothDevices() {
return new Promise<any>((resolve, reject) => {
this.win.DatecsPrinter.listBluetoothDevices((success) => resolve(success), (error) => reject(error));
});
}

connect(deviceAddress: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
setTimeout(() => this.win.DatecsPrinter.connect(deviceAddress, (success) => resolve(success), (error) => reject(error)), this.defaultTimeout);
});
}

printText(text: string, charset: string = 'ISO-8859-1'): Promise<any> {
return new Promise<any>((resolve, reject) => {
this.win.DatecsPrinter.printText(text, charset, (success) => resolve(success), (error) => reject(error));
});
}

最佳答案

您的所有代码都是异步的,因此,所有这些代码将同时执行。

  this.print.connect('00:02:5B:B4:7C:3A').then(result => {
console.log(JSON.stringify(result));
this.print = result;
}).catch(err => {

});


var printStr2 = "";
printStr2 += " {br}";
printStr2 += "{reset}{left}Nome: ";
printStr2 += "ADD VARIAVEL NOME {br}";
printStr2 += "CPF: ";
printStr2 += "ADD VARIAVEL CPF {br}";

//Promises I want to chain
this.print.printText(printStr, 'ISO-8859-1');
this.print.printText(printStr2, 'ISO-8859-1');

this.print.feedPaper(100).then(result => {
console.log(JSON.stringify(result));
this.print = result;
}).catch(err => {

});

为了正确工作,您应该将所有代码放在第一个 Promise 中,如下所示:

this.print.connect('00:02:5B:B4:7C:3A').then(result => {
console.log(JSON.stringify(result));
this.print = result;

// ALL YOUR CODE HERE!!

}).catch(err => {

});

关于javascript - 无法在 Ionic 3 中链式 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49195307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com