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.

does axios have an equivalent to jQuery `when`?

See original GitHub issue

Summary

$.when(data) can directly resolve data as promise. Does axios have an equivalent? Or I have to use a promise library like bluebird to do this?

Context

  • axios version: 0.17.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
lzl124631xcommented, Jan 8, 2018

@raygesualdo Thanks. The ES6 Promises is basically the same as the ones in bluebird. The async/await is so cool.

To sum up, use one of the following options:

  • ES6 Promise
  • ES7 async/await
  • An promise library e.g. bluebird.

Thank you all.

0reactions
raygesualdocommented, Jan 8, 2018

If you use ES6 Promises, it could be re-written like this:

function wechatLogin() {
  var code = util.getQuery('code')
  var token = cache.getJSON(code)
  return new Promise((resolve, reject) => {
    if (token) {
      resolve(token)
    } else {
      post('/wechat/login', {
        code: code,
      })
        .then(res => {
          token = res.data
          cache.setJSON(code, token)
          resolve(token)
        })
        .catch(err => {
          reject()
        })
    }
  })
}

Or, if you’re using async/await:

async function wechatLogin() {
  const code = util.getQuery('code')
  const token = cache.getJSON(code)
  if (token) {
    return token
  }
  const response = await post('/wechat/login', {
    code: code,
  })
  cache.setJSON(code, response.data)
  return response.data
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery ajaxComplete equivalent in axios
jQuery's $.ajax has a function build in where it fires an event when a request has been finished. This is the ajaxComplete event....
Read more >
jQuery, Ajax, Axios
Axios is a promise-based HTTP Client for node.js and the browser. It is isomorphic (= it can run in the browser and node.js...
Read more >
axios vs jQuery | What are the differences?
axios - Promise based HTTP client for the browser and node.js. jQuery - The Write Less, Do More, JavaScript Library..
Read more >
5 Best axios Alternative Tools For GET and POST Requests
1. Fetch API · jQuery is relatively easy to use. · jQuery is also incredibly adaptable since it allows users to install plugins....
Read more >
Comparing different ways to make HTTP requests in ...
Axios · Uses promises to avoid callback hell · It works on both Browser and Nodejs · Support for upload progress · Can...
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