不能在Ionic 3中链接承诺

时间:2018-03-09 13:52:16

标签: javascript cordova typescript ionic3

我有一个插件,cordova-plugin-datecs-printer,有了它我可以在热敏打印机上打印一些条纹。问题是我无法以这种方式打印大量字符串。我已经被插件创建者告知我错误是我处理承诺的方式,但我已经尝试改变我以不同方式处理它们的方式而我没有得到它。我试图寻找一些例子,并了解更多关于承诺的内容,但我不能对此案适用任何内容。

我的代码:

 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 => {

        });

编辑:提供商I使用(打印):

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));
    });
  }

3 个答案:

答案 0 :(得分:1)

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

  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 => {

    });

要正常工作,您应该将所有代码放在第一个承诺中,如下所示:

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 => {

});

答案 1 :(得分:0)

#include <pybind11/pybind11.h> #include <iostream> namespace py = pybind11; void parse_args(int argn__, char** argv__) { for(int i = 1; i < argn__; ++i) { std::cout<< argv__[i]; } } PYBIND11_MODULE(argv_bind, m) { m.def("parse_args", &parse_args); } 基本上就是字面意思。这样做,然后这样做

.then

等。显然,我提供的代码来自一个不了解你的print api应该如何工作的地方,你必须阅读文档才能正确地完成它。

你一定要努力适应承诺。花一点时间阅读一些关于它们的文章。

答案 2 :(得分:0)

通过将所有文本和变量添加到唯一字符串中解决了该问题。如果字符数接近或超过7000,则问题将返回。