Feature/8 return a promise #5
2 changed files with 31 additions and 8 deletions
20
index.js
20
index.js
|
@ -33,13 +33,25 @@ function randomBunny(subreddit, sortBy, cb) {
|
||||||
const random = getRandom(0, size - 1);
|
const random = getRandom(0, size - 1);
|
||||||
|
|
||||||
// Get variables from json to pass back
|
// Get variables from json to pass back
|
||||||
const image = data[random].data['url'];
|
const randomSelect = data[random].data;
|
||||||
const title = data[random].data['title'];
|
|
||||||
|
// The json string to send back
|
||||||
|
const json = {
|
||||||
|
archived: randomSelect['archived'],
|
||||||
|
downs: randomSelect['downs'],
|
||||||
|
hidden: randomSelect['hidden'],
|
||||||
|
permalink: randomSelect['permalink'],
|
||||||
|
subreddit: randomSelect['subreddit'],
|
||||||
|
subredditSubscribers: randomSelect['subreddit_subscribers'],
|
||||||
|
title: randomSelect['title'],
|
||||||
|
ups: randomSelect['ups'],
|
||||||
|
url: randomSelect['url']
|
||||||
|
};
|
||||||
|
|
||||||
// If the post is a .jpg, send back the data and stop looping
|
// If the post is a .jpg, send back the data and stop looping
|
||||||
if (image.includes('.jpg')) {
|
if (json.url.includes('.jpg')) {
|
||||||
found = true;
|
found = true;
|
||||||
cb(image, title);
|
cb(json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
19
readme.md
19
readme.md
|
@ -14,8 +14,8 @@ $ npm install --save random-bunny
|
||||||
```js
|
```js
|
||||||
const randomBunny = require('random-bunny');
|
const randomBunny = require('random-bunny');
|
||||||
|
|
||||||
randomBunny('rabbits', 'new', (image, title) => {
|
randomBunny('rabbits', 'new', res => {
|
||||||
console.log(title + ": " + image);
|
console.log(res.title + ": " + res.url);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -24,7 +24,18 @@ randomBunny('rabbits', 'new', (image, title) => {
|
||||||
|
|
||||||
### `randomBunny()`
|
### `randomBunny()`
|
||||||
|
|
||||||
Returns a `url` and `title` for a random post to the `callback`. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `callback(image, title)`
|
Returns a `json string` for a random post to the `callback`. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `callback(res)`
|
||||||
|
|
||||||
|
The json string which gets returned consists of:
|
||||||
|
- archived
|
||||||
|
- downs
|
||||||
|
- hidden
|
||||||
|
- permalink
|
||||||
|
- subreddit
|
||||||
|
- subredditSubscribers
|
||||||
|
- title
|
||||||
|
- ups
|
||||||
|
- url
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
@ -37,4 +48,4 @@ Returns a `url` and `title` for a random post to the `callback`. Accepts 3 argum
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes);
|
MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes)
|
Loading…
Reference in a new issue