Skip to content

Instantly share code, notes, and snippets.

@shhider
Created October 25, 2017 10:21
Show Gist options
  • Save shhider/5aedd80e8797cc5f1f05a8bce3795be0 to your computer and use it in GitHub Desktop.
Save shhider/5aedd80e8797cc5f1f05a8bce3795be0 to your computer and use it in GitHub Desktop.

understand await/async in ES7

// example
function readread (isReject) {
  return new Promise(function (resolve, reject) {
    setTimeout(() => {
      if (isReject) {
        reject('reject reject');
      } else {
        resolve('resolve resolve');
      }
    }, 400);
  });
}

async function main (isReject) {
  // let result = await readread(isReject);
  let result;
  try {
    result = await readread(isReject);
  } catch (e) {
    result = 'reject catched';
  }
  return result;
}

main(true)
  .then(result => console.log('ok', result))
  .catch(error => console.log('error', error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment