Feature/8 return a promise #5

Merged
Vylpes merged 7 commits from feature/8-return-a-promise into develop 2021-04-12 20:22:57 +01:00
2 changed files with 31 additions and 8 deletions
Showing only changes of commit 447ec5c590 - Show all commits

View file

@ -33,13 +33,25 @@ function randomBunny(subreddit, sortBy, cb) {
const random = getRandom(0, size - 1);
// Get variables from json to pass back
const image = data[random].data['url'];
const title = data[random].data['title'];
const randomSelect = data[random].data;
// 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 (image.includes('.jpg')) {
if (json.url.includes('.jpg')) {
found = true;
cb(image, title);
cb(json);
}
}
});

View file

@ -14,8 +14,8 @@ $ npm install --save random-bunny
```js
const randomBunny = require('random-bunny');
randomBunny('rabbits', 'new', (image, title) => {
console.log(title + ": " + image);
randomBunny('rabbits', 'new', res => {
console.log(res.title + ": " + res.url);
});
```
@ -24,7 +24,18 @@ randomBunny('rabbits', 'new', (image, title) => {
### `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
@ -37,4 +48,4 @@ Returns a `url` and `title` for a random post to the `callback`. Accepts 3 argum
## License
MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes);
MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes)