Skip to content

Commit

Permalink
fixup: callback affiliation for multiple TokenRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
KonStg committed Feb 20, 2023
1 parent e9313a4 commit ed70101
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/token-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ TokenCache.prototype.authenticate = require('./auth').authenticate;
*/
function TokenRequest(authenticate, options) {

var self = this;
this.status = 'expired';
this.pendingCallbacks = [];

// execute accumulated callbacks during the 'pending' state
function fireCallbacks(err, token) {
self.pendingCallbacks.forEach(function (callback) {
TokenRequest.prototype.fireCallbacks = function (err, token) {
this.pendingCallbacks.forEach(function (callback) {
callback(err, token);
});
self.pendingCallbacks = [];
this.pendingCallbacks = [];
}

TokenRequest.prototype.get = function (callback) {
var self = this;

if (self.status == 'expired') {

Expand All @@ -75,13 +75,13 @@ function TokenRequest(authenticate, options) {
authenticate(options, function (err, token) {
if (err) {
self.status = 'expired';
return fireCallbacks(err, null);
return self.fireCallbacks(err, null);
}
self.issued = Date.now();
self.duration = options.expiration || 60 * 60 * 1000;
self.token = token;
self.status = 'completed';
return fireCallbacks(null, token);
return self.fireCallbacks(null, token);
});

} else if (self.status == 'pending') {
Expand Down

0 comments on commit ed70101

Please sign in to comment.