Javascript (角度)Js-如何简化承诺结果?
我来自嵌入式C背景,自学AngularJs 1.x;我怀疑我的问题是一般的JS问题,而不是特定于AngularJs的问题 我发现这种模式在我的代码中重复:Javascript (角度)Js-如何简化承诺结果?,javascript,angularjs,promise,angular-promise,Javascript,Angularjs,Promise,Angular Promise,我来自嵌入式C背景,自学AngularJs 1.x;我怀疑我的问题是一般的JS问题,而不是特定于AngularJs的问题 我发现这种模式在我的代码中重复: $http.get(url) .success(function(data, status, headers, config) { console.log('Success');
$http.get(url)
.success(function(data, status, headers, config)
{
console.log('Success');
});
})
.error(function(data, status, headers, config)
{
console.error('Error !');
});
作为一个C类的人,我和lambdas不一样,尽管我对回调很满意
成功和失败部分的代码可能非常大,这使得代码看起来很混乱——尤其是看到所有这些代码都作为参数传递
我猜$http
的参数是/是(a)承诺
有什么方法可以使代码更模块化,更易于阅读和维护吗
例如,我想象我可以声明一些成功/失败函数&调用这些函数,如下所示:
function it_succded(data, status, headers, config))
{
console.log('Success');
});
function it_failed(data, status, headers, config)
{
console.error('Error !');
});
$http.get(url)
.success(function(data, status, headers, config)
{
it_succded(data, status, headers, config))
});
})
.error(function(data, status, headers, config)
{
it_failed(data, status, headers, config)
});
});
当然,我可以只编写代码&看,但在这里提问是因为我想学习,并希望真正理解这一点的人给出解释,最好是专业编写(Angular)Js的人。对于使用promise界面,你不应该使用
.success()
或或.error()
:它们是。相反,请使用.then()
和catch()
。这些回调接收的参数略有不同:
$http.get(url).then(function(response) {
console.log("Success", response.data);
}).catch(function(response) {
console.log("Error", response.status);
});
响应
是一个对象,具有预期的:
数据
–{string | Object}
–使用转换函数转换的响应体
状态
–{number}
–响应的HTTP状态代码
标题
–{function([headerName])}
–标题获取函数
config
–{Object}
–用于生成请求的配置对象
statusText
–{string}
–响应的HTTP状态文本
xhrStatus
–{string}
–XMLHttpRequest的状态(完成、错误、超时或中止)
实际上,您可以单独定义回调函数,然后回调参数只能是函数引用:
function it_succded(response) {
console.log("Success", response.data);
}
function it_failed(response) {
console.log("HTTP Error", response.status, response.statusText);
}
$http.get(url).then(it_succded).catch(it_failed);
您可以将这些函数定义为方法,例如在$scope
对象上:
$scope.it_succded = function (response) {
console.log("Success", response.data);
$scope.data = response.data;
}
$scope.it_failed = function (response) {
console.log("HTTP Error", response.status, response.statusText);
}
$http.get(url).then($scope.it_succded).catch($scope.it_failed);
请注意:请注意,当Promise实现调用这些回调时,不会设置此
。因此,不要在它们中使用this
,或者将它们定义为箭头函数(this
将是词汇上下文中的任何函数),将函数绑定到特定的this
对象,或者提供一些包装回调:
.then(function(response) {
return $scope.it_succded(response);
})
是的,你可以
$http({
your configs
})
.then(funtion(result){ // function on success
return result.data // this is important as AngularJS $http will bind the data from the server into this variable
},
function(errorResponse){
if(errorResponse.status === 401){
// your code for handling 401 errors
}
else if(errorResponse.status === 404){
// your code for handling 404 errors
}
else if(errorResponse.status === 500){
// your code for handling 500 errors
}
})
.then(function(data){
// another function to process data
return data
})
.then(function(data){
// yet another function to process data
return data
})
我们可以根据需要组合任意多个。
您可以通过此链接阅读更多有关承诺的信息:
希望它能对您有所帮助。请参阅;$http方法返回承诺,参数是一个字符串。我不知道成功和失败的方法是什么,承诺已经实现了。这些函数的参数是回调函数,是的,您可以提取它们(但是function(args){callback(args);}
除了接受任何返回值之外没有任何其他功能;只需传递callback
)。直到1.4.3版,.success语法都是正确的。请看这里的旧文档:看起来我猜对了,但是有这么一位高代表性的用户来确认它真是太好了。谢谢我将在24小时内开放此fr,以吸引其他答案,帮助像我这样的其他N00B,然后奖励答案。我不认为您可以更新您的答案以声明it成功
和it失败
为$scope
函数?您的问题实际上是一个通用的JS问题(简化承诺回调),因此,将其与AngularJS概念捆绑在一起似乎是不正确的?Angular1中唯一不支持的是解构(我们现在是版本7),因此不要执行函数({data,status})
,而是函数(响应)
。并处理诸如响应.data
,…等属性。我更新了我的答案以使用这种语法。根据您的实际情况,有几种可能性。例如,可以使用bind
。请注意,Promise实现将使用一个额外参数(响应)调用函数。如果你对这个问题有一个特别的问题,而你找不到答案,请考虑发布一个新的问题。