Skip to content

Instantly share code, notes, and snippets.

@cqfd
Created July 7, 2015 23:31
Show Gist options
  • Save cqfd/c5454e54dc9860a1bf39 to your computer and use it in GitHub Desktop.
Save cqfd/c5454e54dc9860a1bf39 to your computer and use it in GitHub Desktop.
Async/await example.
/*
* To run:
* npm install -g babel
*
* babel-node --stage 0 this-script.js
*/
import fs from 'fs'
const mkDir = makePromisey(::fs.mkdir)
async function mkDirs(dirSegments) {
let path = '.'
for (let dirSegment of dirSegments) {
path = path + '/' + dirSegment
await mkDir(path)
}
}
mkDirs(['a', 'b', 'c']).then(() => console.log('all done!'), e => console.log(`wtf: ${e}`))
function makePromisey(callbackyFunc) {
return function(...args) {
return new Promise((ok, notOk) => {
callbackyFunc(...args, (err, ...vals) => {
if (err) notOk(err)
else ok(...vals)
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment