Last active
February 2, 2018 20:08
-
-
Save dotcypress/8c9bee70db9016a5c23ae7fc20bec608 to your computer and use it in GitHub Desktop.
ZEIT's Micro meets Futures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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