Skip to content

Instantly share code, notes, and snippets.

@yamafaktory
Last active December 24, 2015 16:10
Show Gist options
  • Save yamafaktory/0412a7753da948b25e05 to your computer and use it in GitHub Desktop.
Save yamafaktory/0412a7753da948b25e05 to your computer and use it in GitHub Desktop.
Simple IO module.
;(function () {
'use strict'
/*
Simple IO module.
*/
module.exports = IO
const fs = require('fs')
IO.prototype.create = path => {
return new Promise((res, rej) => {
fs.open(path, 'w', e => {
if (e) rej(e)
res()
})
})
}
IO.prototype.remove = path => {
return new Promise((res, rej) => {
fs.unlink(path, e => {
if (e) rej(e)
res()
})
})
}
IO.prototype.read = path => {
return new Promise((res, rej) => {
fs.readFile(path, 'utf8', (e, data) => {
if (e) rej(e)
res(data)
})
})
}
IO.prototype.write = (path, data) => {
return new Promise((res, rej) => {
fs.writeFile(path, data, e => {
if (e) rej(e)
res()
})
})
}
function IO () {}
}())
@yamafaktory
Copy link
Author

Basic usage:

const IO = require('./io')
const io = Object.create(IO.prototype)
const file = './path/to/file'

yield io.create(file)
yield io.write(file, 'some text')
const data = yield io.read(file)
console.log(data) // 'some text'
yield io.remove(path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment