0

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:

if (final.error !== undefined) {
                console.log("Initial authentication:", final.error_description, "Please refresh the authentication grant");
                extAuthCallback(84);
            } else {
                tokens.set('access_token', final.access_token)
                    .set('expires_in', final.expires_in)
                    .set('refresh_token', final.refresh_token)
                    .set('refresh_date', moment())
                    .write()
                extAuthCallback(1);
            }
        }); 

Who performs this function:

function extAuthCallback(result) {
    return result;
}

And which is called by this variable:

let authentication = auth.extAuth(access_token, auth.extAuthCallback);

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.

dolor3sh4ze
  • 925
  • 1
  • 7
  • 25
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jonrsharpe Nov 27 '20 at 23:07
  • I've seen this post, but that not answer my question – dolor3sh4ze Nov 27 '20 at 23:08
  • sorry but you can't return a value like that. You should use the async await method if you want something like that. – JMSalazarDev Nov 27 '20 at 23:12

1 Answers1

0

You could use a promise, would need to use an async function as well though.

function asyncExtAuth(access_token) {
  return new Promise(resolve => {
    auth.extAuth(access_token, resolve);
  });
}
let authentication = await asyncExtAuth(access_token);
Tom
  • 159
  • 14
  • Making a Promise will not solve the problem since the `auth.extAuth` function does not return `extAuthCallback`. – dolor3sh4ze Nov 27 '20 at 23:05
  • When you call `auth.extAuth` `auth.ExtAuthCallback` is the callback that gets called at the top right? By replacing this with `resolve` the promise will resolve to whatever value `extAuthCallback` is called with. It doesn't need to return the callback. – Tom Nov 27 '20 at 23:08
  • Okay this snippet works, but I can't understand how this works even with the knowledge of what a bride is... Thank's – dolor3sh4ze Nov 27 '20 at 23:13
  • 1
    `resolve` is a function that will "resolve" the promise with a value, whatever you call resolve will be the value returned (e.g. `resolve(5)` would mean the promise resolves to `5`). By passing this function as the `extAuthCallback` paramater when it is called in your other function it resolves the promise to the value. Traditionally you would have to use `.then` to then use this resolved value but by using an async function you can `await` the promise resolving, this stops execution of that function and waits for the promise to resolve before returning the resolved value. – Tom Nov 27 '20 at 23:19