const Task = run => ({ // (a -> e) -> (a -> r) -> Task(a) // Might be inaccurate idk run, // undefined -> undefined log: () => run(console.error) (console.log), // (a -> b) -> Task(b) map: f => Task(err => res => run(err) (x => res(f(x)) )), // (a -> Task(b)) -> Task(b) runMap: f => Task(err => res => run(err) (x => f(x).run(err) (res) )) }) /* Sample Usage: const http = (uri, method = "GET") => Task(err => res => { const xhr = new XMLHttpRequest() xhr.open(method, uri, true) xhr.onload = () => (xhr.status < 300 && xhr.status >= 200) ? res(xhr.response) : err(xhr.status + " " + xhr.statusText) xhr.onerror = err xhr.send() }) app = http("https://randomuser.me/api") .map(JSON.parse) .map(x => x.results) // Run it when you want app.run(err => console.error(err)) (res => console.log(res)) // Shorthand for convenience app.log() */