如何返回回调函数?

时间:2021-10-12 16:05:01

I using Angular's $http to get questions, which expects a promise in return, either success or error:

我使用角的$http获取问题,它期望得到一个承诺的回报,要么成功,要么失败:

Question.getQuestions().success(function (res) {
    ...
}).error(function (err) {});

In my method, I need to check if token is expired, if so, refresh, then make the request to /questions and return the promise. Otherwise, just make the request to /questions as usual:

在我的方法中,我需要检查令牌是否已过期,如果是,刷新,然后发出请求/问题并返回承诺。否则,像往常一样提出/提问:

getQuestions: function() {

   // this is called by refreshToken but not returning the $http promise
    var get = function() {
        return $http({
            method: 'GET',
            url: url + ver + '/questions',
            ...
        });
    };

    if (Auth.tokenIsExpired()) {
        return Auth.refreshToken(get);
    } else {
        return get();
    }
},

The refreshToken is another $http which relies on a promise, then calls the get() callback.

refreshToken是另一个$http,它依赖于一个承诺,然后调用get()回调。

Auth {...

    refreshToken: function(callback) {
        ...
        _this.getAuth(OAuth).success(function (access_obj) {
            //set token
            callback();
        })...

Where getAuth is another $http promise:

getAuth是另一个$http承诺:

    getAuth: function(params) {
            return $http({
                method: 'POST',
                url: url + '/oauth/access_token',
                ...
            });
        },

All of these methods are called as expected, but am getting error:

所有这些方法都被称为预期,但都是错误的:

Cannot read property 'success' of undefined

不能读取未定义的属性“success”

This is because Auth.refreshToken(get) is not returning the /questions $http call as it should. How can I return the promise from that back to the original Question.getQuestions()?

这是因为Auth.refreshToken(get)没有按其应有的方式返回/questions $http调用。我怎样才能把这个承诺回复到最初的问题。getquestions ()?

2 个解决方案

#1


2  

Here's a suggestion since your code looks over-complicated.

这里有一个建议,因为您的代码看起来过于复杂。

getQuestions: function() {   
    var get = $http({
        method: 'GET',
        url: url + ver + '/questions',
        ...
    }); 
    if (Auth.tokenIsExpired()) {
        return Auth.refreshToken().then(function(){
         return get();
        });
    } else {
        return get();
    }
}

refreshToken: function() {
    return _this.getAuth(OAuth).then(function (access_obj) {
        //set token
    });
}

Basically refreshToken should refresh the token and return a promise for when its done. Then when you GET the questions you check if the token is expired, if it did then you call refreshToken and when its done (.success()/.then() is called you do the usual GET)

基本上refreshToken将刷新令牌并返回一个承诺。然后,当您收到问题时,您检查这个令牌是否过期,如果过期了,您将调用refreshToken,当它完成时(.success()/ Then()将被调用,您执行通常的GET操作)

You can simplify this further and let Auth handle all of the token refreshing for you by moving the line Auth.tokenIsExpired() inside the refreshToken method.

您可以进一步简化此操作,并让Auth通过在refreshToken方法中移动Auth. tokenisexpired()行来为您处理刷新的所有令牌。

Another example:

另一个例子:

getQuestions: function() {    
    return Auth.refreshToken().then(function(){
         return $http.get(url + ver + '/questions');
    });
}

refreshToken: function() {
    if (_this.tokenIsExpired())
     return _this.getAuth(OAuth).then(function (access_obj) {
        //set token
    });
    else return $q.resolve(/*value needed?*/);
}

#2


0  

This is failing because when you refresh the authToken you just call the get but don't return the promise it returns.

这是失败的,因为当您刷新authToken时,您只需调用get,但不返回它返回的承诺。

Try this:

试试这个:

refreshToken: function(callback) {
    ...
    _this.getAuth(OAuth).success(function (access_obj) {
        //set token
        return callback();
    })...

but I would not mix callbacks and promises, would instead do:

但我不会把回调和承诺混在一起,而是:

refreshToken: function(callback) {
    var promise = $q.defer();
    _this.getAuth(OAuth).success(function (access_obj) {
        //set token
        promise.resolve();
    })...

then:

然后:

if (Auth.tokenIsExpired()) {
    Auth.refreshToken().then(function() { return get() });
} else {
    return get();
}

Most preferable to clean this up would be to make the token check return a promise itself that resolves once it confirms or refreshes the token so the final would just be:

最可取的清理方法是让令令牌检查返回一个承诺本身,一旦它确认或刷新令牌,该承诺本身就会解决,因此最终的结果将是:

Auth.checkToken().then(function() { return get() });

#1


2  

Here's a suggestion since your code looks over-complicated.

这里有一个建议,因为您的代码看起来过于复杂。

getQuestions: function() {   
    var get = $http({
        method: 'GET',
        url: url + ver + '/questions',
        ...
    }); 
    if (Auth.tokenIsExpired()) {
        return Auth.refreshToken().then(function(){
         return get();
        });
    } else {
        return get();
    }
}

refreshToken: function() {
    return _this.getAuth(OAuth).then(function (access_obj) {
        //set token
    });
}

Basically refreshToken should refresh the token and return a promise for when its done. Then when you GET the questions you check if the token is expired, if it did then you call refreshToken and when its done (.success()/.then() is called you do the usual GET)

基本上refreshToken将刷新令牌并返回一个承诺。然后,当您收到问题时,您检查这个令牌是否过期,如果过期了,您将调用refreshToken,当它完成时(.success()/ Then()将被调用,您执行通常的GET操作)

You can simplify this further and let Auth handle all of the token refreshing for you by moving the line Auth.tokenIsExpired() inside the refreshToken method.

您可以进一步简化此操作,并让Auth通过在refreshToken方法中移动Auth. tokenisexpired()行来为您处理刷新的所有令牌。

Another example:

另一个例子:

getQuestions: function() {    
    return Auth.refreshToken().then(function(){
         return $http.get(url + ver + '/questions');
    });
}

refreshToken: function() {
    if (_this.tokenIsExpired())
     return _this.getAuth(OAuth).then(function (access_obj) {
        //set token
    });
    else return $q.resolve(/*value needed?*/);
}

#2


0  

This is failing because when you refresh the authToken you just call the get but don't return the promise it returns.

这是失败的,因为当您刷新authToken时,您只需调用get,但不返回它返回的承诺。

Try this:

试试这个:

refreshToken: function(callback) {
    ...
    _this.getAuth(OAuth).success(function (access_obj) {
        //set token
        return callback();
    })...

but I would not mix callbacks and promises, would instead do:

但我不会把回调和承诺混在一起,而是:

refreshToken: function(callback) {
    var promise = $q.defer();
    _this.getAuth(OAuth).success(function (access_obj) {
        //set token
        promise.resolve();
    })...

then:

然后:

if (Auth.tokenIsExpired()) {
    Auth.refreshToken().then(function() { return get() });
} else {
    return get();
}

Most preferable to clean this up would be to make the token check return a promise itself that resolves once it confirms or refreshes the token so the final would just be:

最可取的清理方法是让令令牌检查返回一个承诺本身,一旦它确认或刷新令牌,该承诺本身就会解决,因此最终的结果将是:

Auth.checkToken().then(function() { return get() });