Last active
March 27, 2016 12:21
-
-
Save oliverturner/7e66a99f9a9fc0adb031 to your computer and use it in GitHub Desktop.
awaitable fs functions adapted from https://twitter.com/jamespearce/status/644002032479662080
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
import fs from 'fs' | |
import makeAwaitable from './makeAwaitable' | |
const fs2 = { | |
open: makeAwaitable(fs.open), | |
write: makeAwaitable(fs.write), | |
close: makeAwaitable(fs.close) | |
} | |
async function save (fileName, text) { | |
const file = await fs2.open(fileName, 'w') | |
await fs2.write(file, text) | |
await fs2.close(file) | |
} | |
save('./test.txt', 'w00t!') |
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
export const makeAwaitable = (fn) => { | |
(...args) => new Promise((resolve, reject) => | |
fn.call(null, ...args, (err, ...args) => { | |
err ? reject(err) : resolve(args) | |
}) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment