Skip to content

Commit

Permalink
Set a reasonable timeout on request
Browse files Browse the repository at this point in the history
  • Loading branch information
baer committed May 16, 2017
1 parent 9e735df commit b873533
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/get-advice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ const request = require("request-promise-native");

const SUBREDDIT = "WorstAdviceEver";

const requestDefaultOptions = {
json: true, // Automatically parses the JSON string in the response,
timeout: 5000
};

const getRandomPost = (subreddit) => {
return request({
uri: `http://reddit.com/r/${subreddit}.json`,
json: true // Automatically parses the JSON string in the response
})
return request(Object.assign({}, requestDefaultOptions, {
uri: `http://reddit.com/r/${subreddit}.json`
}))
.then((response) => {
const posts = get(response, "data.children");

Expand All @@ -26,10 +30,9 @@ const getRandomPost = (subreddit) => {
const getRandomResponse = (redditPostURIFragment) => {
const reditPostURI = `http://reddit.com/${redditPostURIFragment}.json`;

return request({
uri: reditPostURI,
json: true // Automatically parses the JSON string in the response
})
return request(Object.assign({}, requestDefaultOptions, {
uri: reditPostURI
}))
.then((response) => {
// Oddly, the response body is an array with two elements where the first is the question node
// and the second is the list of responses
Expand Down

0 comments on commit b873533

Please sign in to comment.