7
I’m using an external library function to acquire the token
of a user, it is the following:
export default function GetGraphToken(
email: string,
password: string,
callback: (graphClient: Client, token: string) => void,
): void {
...
context.acquireTokenWithUsernamePassword(
resource,
email,
password,
clientId,
(tokenError, token) => {
if (tokenError) {
throw new Error(tokenError.message);
}
const tokenResponse = token as TokenResponse;
...
callback(graphClient, tokenResponse.accessToken);
},
);
}
The function callback of acquireTokenWithUsernamePassword
is the one I actually get the token from, but after that I want to expose this token to other functions using it. Currently, the way I’m doing this is by passing another callback to deal with it. How to expect from functions callbacks, this will lead me to use chained function within function and I think it is possible to avoid this using Promise
.
If I could change the function acquireTokenWithUsernamePassword
I would make her return a Promise
instead of receiving a callback. However, as it belongs to an external library, I cannot. Behold, my doubt arises, I am stuck to the use of callback or is there some way to get rid of it using Promise
?
+1 Wrappers So they are wonderful things, even facilitate the creation of tests. Another option is, if it is an open source lib, try to collaborate with the project (creating a Issue on and, if well accepted, a PR) or threaten the repository owner (do not recommend, be a nice person)
– Rafael Tavares
It fell like a glove! Until now I had only found examples, which converted callback to Promise, in codes that the developer could modify and I did not realize that it would also use this technique in third-party libraries.
– Matheus Wallace