es5-async-await

1.0.0 • Public • Published

es5-async-await

npm package build code climate coverage issues dependencies devDependencies downloads

Table of Contents

# 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);

Readme

Keywords

none

Package Sidebar

Install

npm i es5-async-await

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • bakerface