Skip to content

Instantly share code, notes, and snippets.

@kwijibo
Created January 26, 2016 14:12
Show Gist options
  • Save kwijibo/39f99671572bf9b2f0b5 to your computer and use it in GitHub Desktop.
Save kwijibo/39f99671572bf9b2f0b5 to your computer and use it in GitHub Desktop.
// httpGET :: url -> HttpGET
const httpGET = (url) => {
let transform = x => x
const GET = {
url: url,
map: (f) => {
transform = x => f(transform(x))
return GET
},
fork: (reject,resolve) => {
request(url, (err,data) => err? reject(err) : resolve(transform(data)))
},
resolve: (val) => transform(val)
}
return GET
}
const getCatPictures = () => httpGET('http://example.com/cats/pics.json')
.map(JSON.parse)
.map(imagesToHtml)
//app usage
getCatPictures()
.map(renderInEl('#gallery'))
.fork(console.log, console.error)
//test usage
const testGET = getCatPictures()
expect(testGET.url).to.be('http://example.com/cats/pics.json')
const fakeResponse = JSON.stringify({image1: 'cat1.jpg', image2: 'cat2.jpg'})
const result = testGET.resolve(fakeResponse)
expect(result).to.be('<ul><li><img src=...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment