我想使用promises,但我有一个回调API,格式如下:
window.onload; // set to callback
...
window.onload = function() {
};
Run Code Online (Sandbox Code Playgroud)
function request(onChangeHandler) {
...
}
request(function() {
// change happened
...
});
Run Code Online (Sandbox Code Playgroud)
function getStuff(dat, callback) {
...
}
getStuff("dataParam", function(err, data) {
...
})
Run Code Online (Sandbox Code Playgroud)
API;
API.one(function(err, data) {
API.two(function(err, data2) {
API.three(function(err, data3) {
...
});
});
});
Run Code Online (Sandbox Code Playgroud)
如何捕获地理位置特定错误以通知用户他们必须打开地理位置?
catch记录了一个名为PositionError的错误,如Mozilla文档" https://developer.mozilla.org/en-US/docs/Web/API/PositionError "中所述.
*注意:我的代码没有捕获错误,它只显示:
Uncaught (in promise) ReferenceError: PositionError is not defined
Run Code Online (Sandbox Code Playgroud)
码
getCurrentLocation() {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
});
},
async inout() {
try {
let location = await this.getCurrentLocation();
let response = await axios.post(API.URL, {});
} catch (e) {
if(e instanceof PositionError) {
console.log('position error')
}
}
}
Run Code Online (Sandbox Code Playgroud)