Skip to content

Latest commit

 

History

History
51 lines (42 loc) · 1.9 KB

README.md

File metadata and controls

51 lines (42 loc) · 1.9 KB

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