- get something from a remote site (
loadPage()
) - find specific element (
findElement()
) - grab and parse contents as JSON (
extractJson()
)
Easy way: grab page and return result
const task = async url => {
// loadPage :: String -> Promise Page
const page = await loadPage(url)
// findElement :: Page -> DomElement|Null
const element = findElement(page)
// extractJson :: DomElement -> Object|Null
return element ? extractJson(element) : null
}
findElement
and extractJson
can return null. It's a perfect case for Maybe
!
const task = async url => {
const page = await loadPage(url)
// findElement :: Page -> Maybe DomElement
// extractJson :: DomElement -> Maybe Object
return findElement(page)
.chain(extractJson)
}
why should I stop on Maybe
. There're more fancy things!
Looks like Task
is the right one for the job.
// loadPage :: String -> Task Error Page
Hmm.. should Task
return data wrapped in Result
? Sounds quite logical!
// loadPage :: String -> Task Error (Result ?? Page)
Does Task
should return Future
... Don't think so. Actually Promise is a Future
so I'm replacing Task
with Future
.
So, I have a Future
which should return Result
. Value of Result
will be parsed and wrapped in Maybe
.
Doest it mean that my function returns Future (Result Error (Maybe object))
? :o
The complexity of this thing weighs heavily on my shoulders...
Ahh! forgot that task()
should return a promise. Way more complicated..
Fuck it! Promise
returning Maybe
is all I need.
going back to stage 2
This definitely does not compile (just written freehand), but here's how I'd go about it: