I’m desperately trying to recover the value of a callback function but I have no idea how to do that. I have a function where I execute this code:
JavaScript
x
13
13
1
if (final.error !== undefined) {
2
console.log("Initial authentication:", final.error_description, "Please refresh the authentication grant");
3
extAuthCallback(84);
4
} else {
5
tokens.set('access_token', final.access_token)
6
.set('expires_in', final.expires_in)
7
.set('refresh_token', final.refresh_token)
8
.set('refresh_date', moment())
9
.write()
10
extAuthCallback(1);
11
}
12
});
13
Who performs this function:
JavaScript
1
4
1
function extAuthCallback(result) {
2
return result;
3
}
4
And which is called by this variable:
JavaScript
1
2
1
let authentication = auth.extAuth(access_token, auth.extAuthCallback);
2
I would like my `authentication’ variable to take the value returned in the callback, and I have no idea how to do that. Returning the callback function to my original function doesn’t work.
Advertisement
Answer
You could use a promise, would need to use an async function as well though.
JavaScript
1
6
1
function asyncExtAuth(access_token) {
2
return new Promise(resolve => {
3
auth.extAuth(access_token, resolve);
4
});
5
}
6
JavaScript
1
2
1
let authentication = await asyncExtAuth(access_token);
2