Skip to content

Instantly share code, notes, and snippets.

@josdejong
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josdejong/944c91721537aa4073dc to your computer and use it in GitHub Desktop.
Save josdejong/944c91721537aa4073dc to your computer and use it in GitHub Desktop.
Test ES7 async/await
{
"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"
}
}
// 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