-
-
Notifications
You must be signed in to change notification settings - Fork 921
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
Add options to response #188
Conversation
Hi. I'm not sure if im doing it the correct way or if you want this in the repo, but this is the solution i found to he following problem: I have a promise code like this: ``` requestsPromise = []; PackofURL.forEach((thisUrl) => { var thisRequest = got(thisUrl.url); requestsPromise.push(thisRequest); }); return Promise.all(requestsPromise) .then((allRequests) => { allRequests.forEach((thisRequest, index) => { if(CheckURLForSomething(thisRequest)) { Requester.doSomething(thisRequest); } }); }); ``` If the request returns `200` i pass it into `CheckURLForSomething` and it will hash the url to store as a key of another part of the code. The problem is that i could not build the full URL (protocol + host + path + query) with the response. I could do something like this: ``` PackofURL.forEach((thisUrl) => { var thisRequest = got(thisUrl.url) .then((response) => { return Promise.resolve({ fullUrl: thisUrl.url, response: response }); }); requestsPromise.push(thisRequest); }); return Promise.all(requestsPromise) .then((allRequests) => { allRequests.forEach((thisRequest, index) => { if(CheckURLForSomething(thisRequest.fullUrl)) { Requester.doSomething(thisRequest.response); } }); }); ``` But maybe more people have the same problem and with this change i can get just `thisRequest.request.href`. If you want this in the code, let me know if i should change anything.
Hi @luanmuniz. Putting options in result can cause troubles, because this will force Node to keep Code you provided is a way to go, but could be a little improved: requestsPromise = PackofURL.map(thisUrl =>
got(thisUrl.url)
.then(response => {fullUrl: thisUrl.url, response: response})
);
return Promise.all(requestsPromise)
.then(allRequests => {
allRequests.forEach((thisRequest, index) => {
if(CheckURLForSomething(thisRequest.fullUrl)) {
Requester.doSomething(thisRequest.response);
}
});
}); |
@floatdrop Its only a few more variables. In this PR at least, its not the http request itself. Doesn't Node need to keep btw: thanks for the suggestion, i will implement it! i always forget |
@floatdrop Not sure it's worth doing, but we could expose just the URL on the result, not the rest of the options. |
@sindresorhus This is the
maybe What the name of the variables should be? Just say the word and i'll update the PR |
#191 looks like a good edge case for this PR – what should be in |
@floatdrop good question. I think the original request since it was the request that was made. |
For simplicity we could add a |
Cool. @luanmuniz Can you update this PR accordingly (and mention it in the docs)? |
Hi. I'm not sure if im doing it the correct way or if you want this in the repo, but this is the solution i found to he following problem:
I have a promise code like this:
If the request returns
200
i pass it intoCheckURLForSomething
and it will hash the url to store as a key of another part of the code.The problem is that i could not build the full URL (protocol + host + path + query) with the response.
I could do something like this:
But maybe more people have the same problem and with this change i can get just
thisRequest.request.href
.If you want this in the code, let me know if i should change anything.