stuppid_bot

Async/await

Dec 18th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Отлов ошибок
  2. function f() {
  3.   return new Promise((resolve, reject) => setTimeout(reject, 0, 'some error'));
  4. }
  5.  
  6. // Можно отлавливать через try/catch
  7. (async function main() {
  8.   try {
  9.     await f();
  10.   } catch (e) {
  11.     console.log(e);
  12.   }
  13. })();
  14.  
  15. // А можно с помощью метода catch, т.к. await-функции возвращают Promise
  16. (async function main() {
  17.   await f();
  18. })().catch(e => console.log(e));
Advertisement
Add Comment
Please, Sign In to add comment