将 2 个函数传递给 '.then()' 是什么意思? (Angular.js)
What does it mean to pass 2 functions into '.then()'? (Angular.js)
我正在阅读一些关于 angular.js 的教程并遇到了这个表达式:
.then(handleRequest, handleRequest)
我想知道将两个相同的函数传递给 .then() 是什么意思?
这里有更多上下文:
function MainCtrl(user, auth) {
var self = this;
function handleRequest(res) {
var token = res.data ? res.data.token : null;
if(token) { console.log('JWT:', token); }
self.message = res.data.message;
}
self.login = function() {
user.login(self.username, self.password)
.then(handleRequest, handleRequest)
}
...
}
angular.module('app', [])
.controller('Main', MainCtrl)
....
})();
原始教程可以在这里找到:https://thinkster.io/angularjs-jwt-auth
then
方法定义为:
promise.then(onFulfilled, onRejected)
第一个参数在承诺实现时调用。
当承诺被拒绝时调用第二个参数。
传递相同的回调函数作为两个参数意味着作者打算使用相同的函数来处理承诺的实现或拒绝。
阅读 the complete spec 了解更多详情。
第一个用于 successCallback,第二个用于 errorCallback。所以
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
承诺有点复杂的模式来理解。对我来说最好的资源是
我正在阅读一些关于 angular.js 的教程并遇到了这个表达式:
.then(handleRequest, handleRequest)
我想知道将两个相同的函数传递给 .then() 是什么意思?
这里有更多上下文:
function MainCtrl(user, auth) {
var self = this;
function handleRequest(res) {
var token = res.data ? res.data.token : null;
if(token) { console.log('JWT:', token); }
self.message = res.data.message;
}
self.login = function() {
user.login(self.username, self.password)
.then(handleRequest, handleRequest)
}
...
}
angular.module('app', [])
.controller('Main', MainCtrl)
....
})();
原始教程可以在这里找到:https://thinkster.io/angularjs-jwt-auth
then
方法定义为:
promise.then(onFulfilled, onRejected)
第一个参数在承诺实现时调用。
当承诺被拒绝时调用第二个参数。
传递相同的回调函数作为两个参数意味着作者打算使用相同的函数来处理承诺的实现或拒绝。
阅读 the complete spec 了解更多详情。
第一个用于 successCallback,第二个用于 errorCallback。所以
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
承诺有点复杂的模式来理解。对我来说最好的资源是