这是我将实现的函数的文档,以便能够通过这个 API 使用 Promise。我将把实现留作练习:
/**
* Tries connecting using bluetooth. Returns a promise.
* If the connection doesn't succeed, the returned promise is rejected.
* If the connection succeeds, the returned promise is resolved with
* an other promise (that we will call disconnectionPromise).
* This disconnectionPromise is never resolved.
* It's rejected once the connection (which has been
* successfully established) fails.
*/
function connect() {
...
}
因此,一个示例用法是:
var blueToothConnection = service.connect();
blueToothConnection.then(function(disconnectionPromise) {
console.log("connection successfully established");
disconnectionPromise.catch(function() {
console.log("connection lost");
});
}).catch(function() {
console.log("impossible to establish a connection");
});