Superagent: Mention async/await usage somewhere in the docs?

Created on 28 Mar 2016  路  16Comments  路  Source: visionmedia/superagent

Superagent fully supports async/await. It's documented here: https://visionmedia.github.io/superagent/#promise-and-generator-support


I think folks would be interested to know that you can already using superagent with async/await like so:

async function () {
  try {
    var response = await request.post('/user/').send({foo: 4})
  } catch (err) {
    // do something with err...
  }
}

Most helpful comment

In 2.0 we support real promises in .then(), so there's no need to wrap .end():

const response = await request.post('/foo');

should just work. I don't mind adding that to the documentation.

All 16 comments

That doesn't seem like something inherent to superagent. You're using bluebird to promisify the callback, and any promise can be used with async/await. Following that logic, you'd include a note about using async/await in _any_ callback based library, which seems unnecessary.

In 2.0 we support real promises in .then(), so there's no need to wrap .end():

const response = await request.post('/foo');

should just work. I don't mind adding that to the documentation.

May I ask what the point of adding the documentation would be? If you're using async/await, you should see that superagent can return a promise and know that you can use async/await on it. If you're not using it, and aren't familiar with async/await, the documentation would be confusing unless you also mention that you need to transpile code from es7 to use it. Seems like more trouble than it is worth IMO.

May I ask what the point of adding the documentation would be?

The point would be to help and encourage people to switch their codebases over to await/async syntax. EDIT: and of course to introduce those who are unfamiliar with it to it and its possibilities.

IMO, that doesn't seem like a concern of this module.

Providing examples at all isn't a concern of this module, but it's a good thing to do. I'm surprised at how this small (and helpful, I thought) suggestion seems to be upsetting you @crookedneighbor. It was not my intent to upset you. If you feel strongly this small example usage is an existential threat of some sort, by all means let me know and I can close this issue, I wouldn't want to cause you harm through it.

FWIW, @pornel's example is great too, it's just that until 2.0 I think superagent doesn't support it, so this could work in its place in the meantime.

Sorry to convey being upset. Not upset, just have strong opinions :smile:

It just seems out of scope to me. Especially for a language feature that is still stage 3, not stage 4. (If it has transitioned to stage 4, please share a link, cause that's very exciting news!)

Let's wait for superagent 2.0 and es7 finalizing the feature.

For anyone that wants to go bleeding edge and not use stable releases of Superagent... here's a promise implementation (uses ES6/ES7 async/await too, with WHATWG's fetch() method) https://github.com/glazedio/frisbee

Looks like this has been added to the docs:

screen shot 2016-08-03 at 1 16 55 pm

example is here..it's another framework but still a good example; I know his this is an old post..just wanna say II agree @taoeffect , always assume the lowest common denominator with developers and if you're showing examples for everything else you should also show async/await. Anyway for anyone who might come across this thread just wanted to pass this along:

https://github.com/thenables/requisition
https://honeycomb.io/blog/2017/04/es7-await/async-and-superagent

Is it possible to contribute to the docs? I'd like to.

All the examples use then() via promises. It's not obvious that you'd get a response object back because it lacks in the docs. Because most the examples are using sueprtest's built-in assertions which the examples only show being called within then(). So the documentation is totally lacking.

I came here by google searching "superagent async/await example", and I would rather to land in documentation, rather than this issue page. Well thought @taoeffect

I can't help the fact that Google likes to dig out closed issues instead of showing actual docs. I've edited the description to redirect lost googlers.

@kornelski I looked at the docs you linked to, and I don't see a single example of using async/await. :-\

Edit: there's one hidden under "using http/2", but that's not what the link that was inserted into my comment points to.

FWIW I no longer use superagent and now use the native fetch API.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t3hmrman picture t3hmrman  路  9Comments

tj picture tj  路  9Comments

kornelski picture kornelski  路  9Comments

vlinder picture vlinder  路  8Comments

kornelski picture kornelski  路  4Comments