【发布时间】:2015-12-29 21:05:50
【问题描述】:
我读过类似的帖子,但没有一个很清楚如何正确地做到这一点。
我了解 Promise 以及它们通常是如何创建的,成功和失败侦听器等待被触发以解决或拒绝。
我不明白的是,当我调用将成功和失败回调作为参数的 API 方法时 - 我如何确定触发了哪个回调,以便我可以拥有它解决还是拒绝?
例如使用此 Web API 并考虑它提供的 navigator.geolocation.getCurrentPosition 方法:
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
console.log('More or less ' + crd.accuracy + ' meters.');
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.getCurrentPosition(success, error, options);
当 API 发回成功或错误时,它会调用我给它的回调之一,但 我不知道它会调用哪一个来解决或拒绝它。
那么问题是: 监听哪个回调被触发的正确方法是什么? ES6 对这种 API 调用的结果有何承诺?
【问题讨论】:
标签: javascript callback promise asp.net-web-api es6-promise