离子如何从提供者的承诺响应?

问题描述 投票:1回答:2

所以我想从一个无极的提供者的反应,但我并没有多少运气。

我从未组件接收到响应,

this.printerService.print(template).then(

            response => {

              console.log(response);

            }, err => {

             console.log(err);
        });

而我的提供商返回true,

print(template): Promise<any> {
  return window.cordova.plugin.zebraprinter.print(address, join,
        function(success) { 

         return true;

        }, function(fail) { 

          return false;
        }
      );
}
angular ionic-framework promise
2个回答
2
投票

不必返回一个承诺是你似乎什么希望。

print(template): Promise<bool> {
    return new Promise(resolve => {
        window.cordova.plugin.zebraprinter.print(address, join,
            success => resolve(true), // invokes .then() with true
            fail => resolve(false) // invokes .then() with false
        );
    });
}

exampleCall() {
    this.printerService.print(template).then(answer => console.log(answer));
}

如果你想的承诺失败,你可以用拒绝的说法。

print(template): Promise<void> {
    return new Promise((resolve, reject) => {
        window.cordova.plugin.zebraprinter.print(address, join,
            success => resolve(), // invokes .then() without a value
            fail => reject() // invokes .catch() without a value
        );
    });
}

exampleCall() {
    this.printerService.print(template)
        .then(() => console.log('success'))
        .catch(() => console.log('fail'));
}

0
投票

一个简单的方法来实现这一目标,是在像这样一个承诺包裹zebraprinter功能:

print(template): Promise<any> {
   return new Promise((resolve,reject)=> {
      window.cordova.plugin.zebraprinter.print(address, join,
       (success) =>  { 

         resolve(success)

        },(fail) => { 

          reject(fail)
        }
      );
   });
}
© www.soinside.com 2019 - 2024. All rights reserved.