Created
January 3, 2017 15:38
-
-
Save adambene/a5419626daf55e46d63793d37fa18d44 to your computer and use it in GitHub Desktop.
Exception handling in ES6 - Transpiled to ES5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// transpiled to ES5 | |
'use strict'; | |
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | |
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | |
var someAsyncOperation = function () { | |
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(path) { | |
return regeneratorRuntime.wrap(function _callee$(_context) { | |
while (1) { | |
switch (_context.prev = _context.next) { | |
case 0: | |
return _context.abrupt('return', new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
if (typeof path === 'string') { | |
resolve('Hello Panda! Path is = ' + path); | |
} else { | |
reject(new Error('Path is not a string. Path (' + (typeof path === 'undefined' ? 'undefined' : _typeof(path)) + ') = ' + path)); | |
} | |
}, 1000); | |
})); | |
case 1: | |
case 'end': | |
return _context.stop(); | |
} | |
} | |
}, _callee, undefined); | |
})); | |
return function someAsyncOperation(_x) { | |
return _ref.apply(this, arguments); | |
}; | |
}(); | |
// the ES6 way | |
_asyncToGenerator(regeneratorRuntime.mark(function _callee2() { | |
var res; | |
return regeneratorRuntime.wrap(function _callee2$(_context2) { | |
while (1) { | |
switch (_context2.prev = _context2.next) { | |
case 0: | |
_context2.prev = 0; | |
_context2.next = 3; | |
return someAsyncOperation(321); | |
case 3: | |
res = _context2.sent; | |
console.log(res); | |
_context2.next = 10; | |
break; | |
case 7: | |
_context2.prev = 7; | |
_context2.t0 = _context2['catch'](0); | |
console.error('(FROM ASYNC/AWAIT) Error cause is:', _context2.t0); | |
case 10: | |
case 'end': | |
return _context2.stop(); | |
} | |
} | |
}, _callee2, undefined, [[0, 7]]); | |
}))(); | |
// the promise way | |
(function () { | |
someAsyncOperation(123).then(function (res) { | |
console.log(res); | |
}).catch(function (error) { | |
console.error('(FROM PROMISE) Error cause is:', error); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment