-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does this library work with await?
#132
Comments
👍 for being progressive!
Since I am not using ES7 yet, could you provide a small code snippet that you tested out and I can paste into the README? |
Code like this seems to work fine: const request = require('request-promise');
async function preparePayment(cents: number): Promise<boolean> {
const options = {
method: `POST`
,json: true
,uri: `https://pay.org/v1/checkouts`
,body: { 'amount': `99` }
};
try {
const response = await request(options);
return Promise.resolve(response.id == 5);
}
catch (error) {
Promise.reject(error);
}
} |
Thanks a lot for the snippet @RichardJECooke ! I will keep this issue open until I added the snippet to the README. |
@RichardJECooke, I have a few questions and suggestions for your code:
so, something like this:
|
Here's some slightly better (almost real) code I'm using in production (without the static types): async preparePayment(cents) {
const options = {
method: 'POST'
,headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
,json: true
,uri: `http://example.com/v1/checkouts`
,form: {
'authentication.userId': `username`,
'amount': cents.toString()
}
};
try {
const response = await request(options);
return Promise.resolve(response);
}
catch (error) {
return Promise.reject(error);
}
} |
@RichardJECooke I think you're right about template strings; no real disadvantage to using them, except maybe that you have to avoid putting You mentioned that superagent won't work unless you explicitly call |
You'll need to use superagent-promise. But I had issues setting headers in GET calls with it so I switched to this. I've been very happy with request-promise. Has worked perfectly so far. |
try {
const response = await request(options);
return Promise.resolve(response);
}
catch (error) {
return Promise.reject(error);
} Isn't that just the same as doing -> const response = await request(options);
return response; Not sure why you would wrap a try / except only to return a promise that the async function is going to do for you anyway.. |
No, that's not the same. It is the same as this though: return request(options); The problem with your example is that you are missing the catch block (which acts the same as .catch() for normal promises). Without it you'll have uncaught unhandled silent exceptions. As for why, because this question is about documentation. And my example clearly shows how to use await with promises using try and catch. |
My understanding, if you mark a function with async, it will always return a promise, unhanded exceptions should then return a rejected promise. Also the |
I think @RichardJECooke origianl code can also just be -> const request = require('request-promise');
async function preparePayment(cents: number): Promise<boolean> {
return (
await request({
method: `POST`
,json: true
,uri: `https://pay.org/v1/checkouts`
,body: { 'amount': `99` }
})
) === 5;
} Here the await isn't pointless as the return is getting transformed into a boolean return. |
@KpjComp is actually correct on this one. The All 3 of the following are identical:
|
This should be closed. I'm currently using the library now with async await on v7.0.0-test2016100609987d242b. It is working. |
@cspotcode |
@buzai it's an async function so it always returns a promise, no matter what. That's how async functions are required to work. So everything is working correctly. |
@cspotcode yes,thank u , just i know it . |
@buzai Where you able to find a solution? If not please post your code snippet and info about which environment you are using. I’ll take a look. |
@analog-nico i just use await to get some info |
The problem is correctly distinguish errors from http request and from other code:
|
@alex-shamshurin |
How do you guys use await with request? I get the following error: const resp = await request(o); I need to block until i have the response from request. |
See my code snippet in the 2nd comment. You can use 'await' only when it's inside a function prefixed with 'async'. This tells Javascript/Typescript to treat 'await' specially (not just as another variable name) |
lol, nice thread. :-D I guess this can closed |
Richard, from a service I am running: And the function has: ...and
I get the error I get the error {"status":"error","message":"Not authorized to access this endpoint"} but instead of getting the error message sent back so I can show it to the user in the browser, I get a console error... Any help would be great. |
Can I just call
const result = await rp(options)
or do I have to callrp(options).then(...)
to actually trigger the request to start?Some libraries, like superagent, won't work without the latter syntax.
It would be wonderful if you added this info to your home page readme.
The text was updated successfully, but these errors were encountered: