Skip to content

Instantly share code, notes, and snippets.

@dsacramone
Last active August 5, 2017 08:14
Show Gist options
  • Save dsacramone/fb9e9f52d7f00086ae0e72196aed9fc4 to your computer and use it in GitHub Desktop.
Save dsacramone/fb9e9f52d7f00086ae0e72196aed9fc4 to your computer and use it in GitHub Desktop.
/*
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