Skip to content

Instantly share code, notes, and snippets.

@dotcypress
Last active February 2, 2018 20:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotcypress/8c9bee70db9016a5c23ae7fc20bec608 to your computer and use it in GitHub Desktop.
Save dotcypress/8c9bee70db9016a5c23ae7fc20bec608 to your computer and use it in GitHub Desktop.
ZEIT's Micro meets Futures
const { readFile } = require('fs')
const { isFuture, node, encase, after } = require('fluture')
const futurize = (handler) => async (req, res) => {
const result = await handler(req, res)
return isFuture(result) ? result.promise() : result
}
module.exports = futurize(
async (req, res) => {
await wait(500)
return after(500, './package.json').chain(getPackageVersion)
}
)
// Helpers
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const getPackageVersion = (file) =>
node(done => { readFile(file, 'utf8', done) })
.chain(encase(JSON.parse))
.map(x => x.version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment