Last active
August 5, 2017 08:14
-
-
Save dsacramone/fb9e9f52d7f00086ae0e72196aed9fc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Our HTML | |
<div id="results"></div><br /> | |
<input id="input" type='text' /> | |
*/ | |
/* | |
Our API call | |
*/ | |
const fetchQuery = search => { | |
return fetch(`https://www.reddit.com/r/${search}/.json?limit=10&show=all`) | |
.then(res => res.json()) | |
.then(({data}) => { | |
return data.children.map(image => image.data.url); | |
}) | |
}; | |
const inputElement = document.querySelector('#input') | |
const resultsElement = document.querySelector('#results') | |
const $input = Rx.Observable.fromEvent(inputElement, 'input') | |
.debounceTime(2000) | |
.map(e => Rx.Observable.fromPromise(fetchQuery(e.target.value))) | |
.flatMap(Rx.Observable.from) | |
.map(response => response.reduce((acc,r) => acc + `<li>${r}</li>`, '')) | |
$input.subscribe( | |
results => resultsElement.innerHTML = `<ul>${results}</ul>` | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment