Last active
August 29, 2015 14:22
-
-
Save josdejong/944c91721537aa4073dc to your computer and use it in GitHub Desktop.
Test ES7 async/await
This file contains 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
{ | |
"name": "es7_async", | |
"version": "0.0.1", | |
"description": "", | |
"license": "ISC", | |
"dependencies": { | |
"asyncawait": "^0.7.4", | |
"babel-core": "^5.4.4", | |
"babel-plugin-typecheck": "0.0.3" | |
} | |
} |
This file contains 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
// test_async.js | |
// | |
// run via: | |
// npm install babel-node -g | |
// npm install | |
// babel-node --optional es7.asyncFunctions ./test_async.js | |
// | |
// note: there is a file .babelrc listing the plugins to be loaded. That's not yet working | |
function timeout (delay) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
resolve('Ok done!'); | |
//reject(new Error('problem')); | |
}, delay); | |
}) | |
} | |
// using promise: | |
// function foo () { | |
// timeout(1000) | |
// .then(function () { | |
// console.log('time is up!'); | |
// }); | |
// .catch(function (err) { | |
// console.log('Error', err); | |
// }); | |
// } | |
// using async/await: | |
async function foo () { | |
console.log('waiting...'); | |
try { | |
var result = await timeout(1000); | |
console.log('result:', result); | |
} catch (err) { | |
console.log(err.toString()); | |
} | |
} | |
foo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment