# async(fn) - create an async function.
# await(promise) - wait for an async function to resolve.
var async = require('es5-async-await/async');
var await = require('es5-async-await/await');
function sleep(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
function getMessageById(id) {
if (id) return Promise.resolve('and you can await values');
return Promise.reject('using try/catch for errors');
}
var example = async(function(ms) {
console.log('you can pass async function arguments');
await(sleep(ms));
var message = await(getMessageById(1234));
console.log(message);
try {
await(getMessageById());
}
catch (e) {
console.log(e);
}
return 'and promises are returned';
});
example(1000)
.then(console.log)
.catch(console.error);