question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asynchronous support: onRequest/onResponse

See original GitHub issue

Hi, is it possible to support “async for extensions before and after communication”?

Corresponding part https://github.com/nuxt-community/axios-module/blob/22a27a4cdf8c60b065a5cbae68e749da69bf9974/lib/plugin.js#L23-L28

Use Case We are using JWT of Firebase Authentication. We would like to extend the process of granting “Bearer tokens” for all or specific communications prior to communications. If the JWT has been expired, the method of getIdToken() updates it to make it a valid token before continuing with the request. Here, communication occurs before communication, and asynchronous processing should be required.

How to fix We have confirmed the operation with the following support, at least in my environment. We cloned this project and passed the test.

lib/plugin.js

  onRequest(fn) {
    this.interceptors.request.use(async config => await fn(config) || config)
  },
  onResponse(fn) {
    this.interceptors.response.use(async response => await fn(response) || response)
  },

Please update as a bug(?) fix if possible, Thank you in advance.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
areindlcommented, Nov 12, 2021

I came across the same error message when using an async function in onRequest. I think the problem might be that async functions by default return a promise (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#return_value) but here you need to return the config object to pass it further to Axios.

Try something like this:

$axios.onRequest(async function (config) {
    try {
            await [SOMETHING]
            console.log('Awaited function finished')
        } catch (error) {
            console.log(error)
        }
    return config
  })

It took me a while to realise because the docs say “these functions don’t have to return anything by default” (https://axios.nuxtjs.org/helpers) but I guess the implicitly returned promise overrides the config object which would otherwise be passed on in the background by default.

You Sir, are a legend! That should definitely be in the docs.

1reaction
insell824commented, Apr 9, 2021

Thank you for your reply.

The code as instructed worked fine.

Just in case, I shared the sample code that I forked and modified. https://codesandbox.io/s/priceless-cartwright-5sgj7?file=/plugins/axios-extension.js

Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous Request-Reply pattern - Azure - Microsoft Learn
The client application makes a synchronous call to the API, triggering a long-running operation on the backend. · The API responds synchronously as...
Read more >
Asynchronous Request-Response Pattern for Non-Blocking ...
The asynchronous request-response pattern allows you to tell a sender that the message has been processed and what the outcome or result was....
Read more >
Microservices: Asynchronous Request Response Pattern
The Requester, initiator of the communication that begins with sending of Request message and then waits for corresponding Response message · The ...
Read more >
Asynchronous Request-Response Pattern - DEV Community ‍ ‍
The Asynchronous Request-Response Pattern lets you decouple backend services processing from a frontend client, where the backend processing ...
Read more >
Invoking a Web Service Using Asynchronous Request ...
When you invoke a Web Service synchronously, the invoking client application waits for the response to return before it can continue with its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found