Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const benchmark = require('benchmark');
- const suite = new benchmark.Suite;
- suite
- .add('async / await with one single try / catch', async function() {
- async function step1() {
- const foo = await step2();
- return foo + 1;
- }
- async function step2() {
- const foo = await step3();
- return foo + 1;
- }
- async function step3() {
- let foo = await step4();
- // Should handle it here but I can't / won't
- return foo + 1;
- }
- async function step4() {
- throw new Error('ayy');
- }
- let result;
- try {
- result = await step1();
- } catch (err) {
- // Handle it
- }
- })
- .add('async / await with nested try / catch', async function() {
- async function step1() {
- const foo = await step2();
- return foo + 1;
- }
- async function step2() {
- const foo = await step3();
- return foo + 1;
- }
- async function step3() {
- let foo = 0;
- try {
- foo = await step4();
- } catch (err) {
- // Recover it
- }
- return foo + 1;
- }
- async function step4() {
- throw new Error('ayy');
- }
- let result;
- try {
- result = await step1();
- } catch (err) {
- // Handle it
- }
- })
- .add('Promise.catch', {
- defer: true,
- fn: function(deferred) {
- function step1() {
- return step2().then(ok => {
- return ok + 1;
- })
- }
- function step2() {
- return step3().then(ok => {
- return ok + 1;
- });
- }
- function step3() {
- return step4()
- .then(ok => {
- return ok + 1;
- })
- .catch(err => {
- return 1;
- })
- }
- function step4() {
- return Promise.reject(new Error('ayy'));
- }
- step1().then(ok => {
- deferred.resolve();
- }).catch(err => {
- // Handle it
- });
- }
- })
- // add listeners
- .on('cycle', function(event) {
- console.log(String(event.target));
- })
- .on('complete', function() {
- console.log('Fastest is ' + this.filter('fastest').map('name'));
- })
- .run({
- async: true
- });
Add Comment
Please, Sign In to add comment