Skip to content

Instantly share code, notes, and snippets.

@bradparker
Last active May 15, 2016 10:33
Show Gist options
  • Save bradparker/d748d2803a824edfbbd0a2dd8ec05909 to your computer and use it in GitHub Desktop.
Save bradparker/d748d2803a824edfbbd0a2dd8ec05909 to your computer and use it in GitHub Desktop.
Task
const fetch = require('node-fetch')
const Task = require('data.task')
const KEY = 'KEY!'
const asteroidRequest = new Task((reject, resolve) => (
fetch(`https://api.nasa.gov/neo/rest/v1/feed?start_date=2016-05-05&end_date=2016-05-05&api_key=${KEY}`)
.then((res) => res.json())
.then(resolve)
.catch(reject)
))
const logged = asteroidRequest.map((res) => {
console.log('FIRST')
return res
})
const anotherAseroidRequest = new Task((reject, resolve) => (
fetch(`https://api.nasa.gov/neo/rest/v1/feed?start_date=2016-05-05&end_date=2016-05-06&api_key=${KEY}`)
.then((res) => res.json())
.then(resolve)
.catch(reject)
))
const anotherLogged = anotherAseroidRequest.map((res) => {
console.log('SECOND')
return res
})
const yetAnotherAseroidRequest = new Task((reject, resolve) => (
fetch(`https://api.nasa.gov/neo/rest/v1/feed?start_date=2016-05-06&end_date=2016-05-07&api_key=${KEY}`)
.then((res) => res.json())
.then(resolve)
.catch(reject)
))
const yetAnotherLogged = yetAnotherAseroidRequest.map((res) => {
console.log('THIRD')
return res
})
const series = logged.chain((a) => anotherLogged.map((b) => [a, b]))
series.fork((err) => {}, (res) => { console.log('SERIES: ', res) })
// concat :: (Semigroup a) => Task a -> Task a -> Task a
const concat = (taskA, taskB) => (
Task.of((a) => (b) => a.concat(b))
.ap(taskA)
.ap(taskB)
)
// all :: (Semigroup a) => [Task a] -> Task a
const all = (tasks) => (
tasks.reduce((acc, task) => (
concat(acc, task)
))
)
const parallel = all([
logged.map(Array),
anotherLogged.map(Array),
yetAnotherLogged.map(Array)
])
parallel.fork((err) => {}, (res) => { console.log('PARALLEL: ', res) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment