我是 JavaScript 的新手,不了解如何正确使用 Promises。
我正在从 ionic 2 (BLE) 的原生插件中调用一个函数:IsEnabled()
它报告设备上是否启用了蓝牙,如果启用了蓝牙,它会返回一个解析的 promise .我尝试了不同的方法,但它们都不起作用:
在 BLEscanner.js 中
blEnabled() {
BLE.isEnabled();
return Promise;
}
在 Home.js 中
var BLicon {
iconColor = function () {
if (BLEscanner.blEnabled().Promise.isFulfilled())
return '#606060'; //the icon displayed is gray
else return '#030303';
};
msg = function () {
if (BLEscanner.blEnabled().Promise.isFulfilled())
return "Bluetooth is ON";
else return "Bluetooth is OFF";
}
}
我将在键入图标时在操作警报中使用 msg
。但我不确定如何使用/开发 isFulfilled()
命令。
最佳答案
BLE.isEnabled() 返回一个 promise :
blEnabled() {
return BLE.isEnabled();//Returns a promise
}
在 Home.js 中你可以这样使用它
BLEscanner.blEnabled().then(() => {
//success
iconColor = '#606060';
msg = "Bluetooth is ON";
}, () =>{
//fail
iconColor = '#030303';
msg = "Bluetooth is OFF";
});
关于javascript - 如何使用返回 promise 的外部方法? (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423116/