Skip to content

Commit 7b490a5

Browse files
committed
fix: getAuthorization returns authorization object or promise, resulting in the key only being read once
1 parent a78d64c commit 7b490a5

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,28 @@
6363
}
6464

6565
async function getAuthorization(key, organization_id) {
66-
if (authorizations.get(key)) {
67-
return authorizations.get(key)
68-
} else {
69-
let authorization = await readAuthorization(key, organization_id);
70-
authorizations.set(key, authorization)
71-
return authorization
66+
// Check if there is a value orpending promise for this key
67+
if (authorizations.has(key)) {
68+
// Return the value or pending promise
69+
return authorizations.get(key);
70+
}
71+
72+
// Create a new promise and store it
73+
const authorizationPromise = readAuthorization(key, organization_id);
74+
authorizations.set(key, authorizationPromise);
75+
76+
try {
77+
// Wait for the authorization to be resolved
78+
const authorization = await authorizationPromise;
79+
80+
// Once resolved, store the resolved authorization in the map
81+
authorizations.set(key, authorization);
82+
83+
return authorization;
84+
} catch (error) {
85+
// Handle errors if necessary
86+
authorizations.delete(key);
87+
throw error;
7288
}
7389
}
7490

0 commit comments

Comments
 (0)