Created
July 7, 2015 23:31
-
-
Save cqfd/c5454e54dc9860a1bf39 to your computer and use it in GitHub Desktop.
Async/await example.
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
/* | |
* 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