leonardofmed
leonardofmed

Reputation: 835

Can't chain promises in Ionic 3

I have a plugin, cordova-plugin-datecs-printer, and I can print some strings with it on a thermal printer. The problem is that I'm not able to print large numbers of strings this way I'm coding. I have already been informed by the plugin creator that the error is in the way I am dealing with the promises, but I have already tried to change the way I deal with them in different ways and I am not getting it. I tried to look for a few examples and learn more about promises, but I can not apply anything to this case.

My code:

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

        });

EDIT: Provider I'm using (print):

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

Upvotes: 0

Views: 469

Answers (3)

leonardofmed
leonardofmed

Reputation: 835

Resolved the issue by adding all texts and variables into a unique string. The problem returns if the number of characters is close or above 7000.

Upvotes: 0

Humberto de Carvalho
Humberto de Carvalho

Reputation: 147

All your code is asynchronous, so, all this code will be executed at the same time.

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

    });

To work correctly, you should put all your code inside the first promise, something like this:

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

});

Upvotes: 1

TKoL
TKoL

Reputation: 13932

.then is basically literally what it says it is. Do this, THEN do this

this.print.printText(printStr, 'ISO-8859-1')
    .then(() => this.print.printText(printStr2, 'ISO-8859-1'))
    .then(() => this.print.feedPaper(100));

etc. Obviously the code I provided is from a place of not understanding how your print api is supposed to work, you have to read the documentation to do it correctly.

You should definitely make an effort to become comfortable with promises. Take a minute and read some articles about them.

Upvotes: 0

Related Questions