1
private isValidURL(url: string) {
var isValid = false;
this.$http.get(url).then(
(data) => {
console.log('success');
isValid = true;
}
).catch(
(reason) => {
console.log('failure ' + reason);
isValid = false;
}
).then(
() => {
return isValid;
}
)
}
private anotherFunc() {
if (!this.isValidURL(url)) {
alert('Wrong URL');
return false;
}
}
if语句中的警报在isValidURL
的函数调用之前执行。我如何确保函数先执行?函数调用获取请求之前运行的Javascript警报
可能的重复[如何返回来自异步调用的响应?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call ) – David
提示:'isValidURL' *本身*不包含'return'语句,因此其结果总是计算为'undefined',即“falsey”。所以'!this.isValidURL()'将*总是*为真。 – David