async-try-catch
Advanced tools
Comparing version 0.1.0 to 0.1.1
# Async Try Catch | ||
# Async Try-Catch | ||
Perform async try catch. | ||
The name says it all: it performs async try catch. | ||
@@ -7,0 +7,0 @@ * License: MIT |
/* | ||
Next Gen Events | ||
Async Try-Catch | ||
@@ -31,3 +31,3 @@ Copyright (c) 2015 - 2016 Cédric Ronvel | ||
var CoreEvents = require( 'events' ) ; | ||
var Events = require( 'events' ) ; | ||
@@ -42,2 +42,23 @@ | ||
// First, backup everything | ||
if ( ! global.Vanilla ) | ||
{ | ||
global.Vanilla = {} ; | ||
if ( ! global.Vanilla.setTimeout ) { global.Vanilla.setTimeout = setTimeout ; } | ||
if ( ! global.Vanilla.setImmediate ) { global.Vanilla.setImmediate = setImmediate ; } | ||
if ( ! global.Vanilla.nextTick ) { global.Vanilla.nextTick = process.nextTick ; } | ||
if ( ! global.Vanilla.eventsOn ) { global.Vanilla.eventsOn = Events.prototype.on ; } | ||
//if ( ! global.Vanilla.Error ) { global.Vanilla.Error = Error ; } | ||
} | ||
// Check if NextGen Events is part of this project | ||
try { | ||
AsyncTryCatch.NextGenEvents = require( 'nextgen-events' ) ; | ||
} catch ( error ) {} | ||
AsyncTryCatch.stack = [] ; | ||
@@ -68,13 +89,12 @@ AsyncTryCatch.substituted = false ; | ||
AsyncTryCatch.stack.push( this ) ; | ||
try { | ||
AsyncTryCatch.stack.push( this ) ; | ||
this.fn() ; | ||
AsyncTryCatch.stack.pop() ; | ||
} | ||
catch ( error ) { | ||
// ------------------------------------------------ Not consistent?????? ------------------------------------------------ | ||
catchFn( error ) ; | ||
AsyncTryCatch.stack.pop() ; | ||
this.callCatchFn( error ) ; | ||
} | ||
AsyncTryCatch.stack.pop() ; | ||
} ; | ||
@@ -84,65 +104,2 @@ | ||
// First, backup everything | ||
if ( ! global.Vanilla ) | ||
{ | ||
global.Vanilla = {} ; | ||
if ( ! global.Vanilla.setTimeout ) { global.Vanilla.setTimeout = setTimeout ; } | ||
if ( ! global.Vanilla.setImmediate ) { global.Vanilla.setImmediate = setImmediate ; } | ||
if ( ! global.Vanilla.nextTick ) { global.Vanilla.nextTick = process.nextTick ; } | ||
if ( ! global.Vanilla.on ) { global.Vanilla.on = CoreEvents.prototype.on ; } | ||
//if ( ! global.Vanilla.Error ) { global.Vanilla.Error = Error ; } | ||
} | ||
AsyncTryCatch.substitute = function substitute() | ||
{ | ||
if ( AsyncTryCatch.substituted ) { return ; } | ||
AsyncTryCatch.substituted = true ; | ||
global.setTimeout = AsyncTryCatch.setTimeout ; | ||
global.setImmediate = AsyncTryCatch.setTimeout ; | ||
process.nextTick = AsyncTryCatch.nextTick ; | ||
CoreEvents.prototype.on = AsyncTryCatch.on ; | ||
CoreEvents.prototype.addListener = AsyncTryCatch.on ; | ||
//global.Error = AsyncTryCatch.Error ; | ||
// Should do that for all error types, cause they will not inherit from the substituted constructor | ||
if ( AsyncTryCatch.NextGenEvents ) | ||
{ | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.ngevOn ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.ngevOn ; | ||
} | ||
} ; | ||
AsyncTryCatch.restore = function restore() | ||
{ | ||
if ( ! AsyncTryCatch.substituted ) { return ; } | ||
AsyncTryCatch.substituted = false ; | ||
global.setTimeout = global.Vanilla.setTimeout ; | ||
global.setImmediate = global.Vanilla.setImmediate ; | ||
process.nextTick = global.Vanilla.nextTick ; | ||
CoreEvents.prototype.on = global.Vanilla.on ; | ||
CoreEvents.prototype.addListener = global.Vanilla.on ; | ||
//global.Error = global.Vanilla.Error ; | ||
if ( AsyncTryCatch.NextGenEvents ) | ||
{ | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.NextGenEvents.addListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.NextGenEvents.addListener ; | ||
} | ||
} ; | ||
// Handle the bubble up | ||
@@ -215,4 +172,4 @@ AsyncTryCatch.prototype.callCatchFn = function callCatchFn( error ) | ||
{ | ||
var args = [ global.Vanilla.on , 1 , null ].concat( Array.from( arguments ) ) ; | ||
AsyncTryCatch.registerWrapper.apply( this , [ global.Vanilla.on , 1 , null ].concat( Array.from( arguments ) ) ) ; | ||
var args = [ global.Vanilla.eventsOn , 1 , null ].concat( Array.from( arguments ) ) ; | ||
AsyncTryCatch.registerWrapper.apply( this , [ global.Vanilla.eventsOn , 1 , null ].concat( Array.from( arguments ) ) ) ; | ||
} ; | ||
@@ -228,52 +185,22 @@ | ||
/* | ||
AsyncTryCatch.setTimeout_ = function setTimeout( fn , time ) | ||
AsyncTryCatch.substitute = function substitute() | ||
{ | ||
//console.log( 'setTimeout:' , fn.name || '<anonymous>' , time , AsyncTryCatch.stack , "\n" ) ; | ||
if ( AsyncTryCatch.substituted ) { return ; } | ||
AsyncTryCatch.substituted = true ; | ||
if ( ! AsyncTryCatch.stack.length ) | ||
{ | ||
global.Vanilla.setTimeout( fn , time ) ; | ||
return ; | ||
} | ||
global.setTimeout = AsyncTryCatch.setTimeout ; | ||
global.setImmediate = AsyncTryCatch.setTimeout ; | ||
process.nextTick = AsyncTryCatch.nextTick ; | ||
var context = AsyncTryCatch.stack[ AsyncTryCatch.stack.length - 1 ] ; | ||
Events.prototype.on = AsyncTryCatch.on ; | ||
Events.prototype.addListener = AsyncTryCatch.on ; | ||
global.Vanilla.setTimeout( function() { | ||
try { | ||
AsyncTryCatch.stack.push( context ) ; | ||
fn() ; | ||
AsyncTryCatch.stack.pop() ; | ||
} | ||
catch ( error ) { | ||
AsyncTryCatch.stack.pop() ; | ||
context.callCatchFn( error ) ; | ||
} | ||
} , time ) ; | ||
} ; | ||
AsyncTryCatch.setImmediate = function setImmediate( fn ) | ||
{ | ||
if ( ! AsyncTryCatch.stack.length ) | ||
//global.Error = AsyncTryCatch.Error ; | ||
// Should do that for all error types, cause they will not inherit from the substituted constructor | ||
if ( AsyncTryCatch.NextGenEvents ) | ||
{ | ||
global.Vanilla.setImmediate( fn ) ; | ||
return ; | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.ngevOn ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.ngevOn ; | ||
} | ||
var context = AsyncTryCatch.stack[ AsyncTryCatch.stack.length - 1 ] ; | ||
global.Vanilla.setImmediate( function() { | ||
try { | ||
AsyncTryCatch.stack.push( context ) ; | ||
fn() ; | ||
AsyncTryCatch.stack.pop() ; | ||
} | ||
catch ( error ) { | ||
AsyncTryCatch.stack.pop() ; | ||
context.callCatchFn( error ) ; | ||
} | ||
} ) ; | ||
} ; | ||
@@ -283,55 +210,25 @@ | ||
AsyncTryCatch.nextTick = function nextTick( fn ) | ||
AsyncTryCatch.restore = function restore() | ||
{ | ||
if ( ! AsyncTryCatch.stack.length ) | ||
{ | ||
global.Vanilla.nextTick.apply( process , arguments ) ; | ||
return ; | ||
} | ||
if ( ! AsyncTryCatch.substituted ) { return ; } | ||
AsyncTryCatch.substituted = false ; | ||
var fnArgs = Array.prototype.slice.call( arguments , 1 ) ; | ||
var context = AsyncTryCatch.stack[ AsyncTryCatch.stack.length - 1 ] ; | ||
global.setTimeout = global.Vanilla.setTimeout ; | ||
global.setImmediate = global.Vanilla.setImmediate ; | ||
process.nextTick = global.Vanilla.nextTick ; | ||
global.Vanilla.nextTick.call( process , function() { | ||
try { | ||
AsyncTryCatch.stack.push( context ) ; | ||
fn.apply( this , fnArgs ) ; | ||
AsyncTryCatch.stack.pop() ; | ||
} | ||
catch ( error ) { | ||
AsyncTryCatch.stack.pop() ; | ||
context.callCatchFn( error ) ; | ||
} | ||
} ) ; | ||
} ; | ||
AsyncTryCatch.on = function on( event , fn ) | ||
{ | ||
var self = this ; | ||
Events.prototype.on = global.Vanilla.eventsOn ; | ||
Events.prototype.addListener = global.Vanilla.eventsOn ; | ||
if ( ! AsyncTryCatch.stack.length ) | ||
//global.Error = global.Vanilla.Error ; | ||
if ( AsyncTryCatch.NextGenEvents ) | ||
{ | ||
global.Vanilla.on.call( self , event , fn ) ; | ||
return ; | ||
AsyncTryCatch.NextGenEvents.prototype.on = AsyncTryCatch.NextGenEvents.addListener ; | ||
AsyncTryCatch.NextGenEvents.prototype.addListener = AsyncTryCatch.NextGenEvents.addListener ; | ||
} | ||
var context = AsyncTryCatch.stack[ AsyncTryCatch.stack.length - 1 ] ; | ||
global.Vanilla.on.call( self , event , function() { | ||
try { | ||
AsyncTryCatch.stack.push( context ) ; | ||
fn.apply( this , arguments ) ; | ||
AsyncTryCatch.stack.pop() ; | ||
} | ||
catch ( error ) { | ||
AsyncTryCatch.stack.pop() ; | ||
context.callCatchFn( error ) ; | ||
} | ||
} ) ; | ||
} ; | ||
*/ | ||
/* | ||
@@ -338,0 +235,0 @@ AsyncTryCatch.Error = function Error( message ) |
{ | ||
"name": "async-try-catch", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Async try catch", | ||
@@ -13,3 +13,4 @@ "main": "lib/AsyncTryCatch.js", | ||
"jshint": "^2.9.2", | ||
"mocha": "^2.5.3" | ||
"mocha": "^2.5.3", | ||
"nextgen-events": "^0.5.14" | ||
}, | ||
@@ -34,3 +35,3 @@ "scripts": { | ||
"copyright": { | ||
"title": "Next-Gen Events", | ||
"title": "Async Try-Catch", | ||
"years": [ | ||
@@ -37,0 +38,0 @@ 2015, |
# Async Try Catch | ||
# Async Try-Catch | ||
Perform async try catch. | ||
The name says it all: it performs async try catch. | ||
@@ -7,0 +7,0 @@ * License: MIT |
/* | ||
Next Gen Events | ||
Async Try-Catch | ||
@@ -35,2 +35,5 @@ Copyright (c) 2015 - 2016 Cédric Ronvel | ||
var Events = require( 'events' ) ; | ||
var NextGenEvents = require( 'nextgen-events' ) ; | ||
var expect = require( 'expect.js' ) ; | ||
@@ -46,3 +49,3 @@ | ||
describe( "Async Try Catch" , function() { | ||
describe( "Synchronous" , function() { | ||
@@ -59,2 +62,23 @@ it( "Sync" , function() { | ||
it( "Sync, nested" , function() { | ||
asyncTry( function() { | ||
asyncTry( function() { | ||
throw new Error( 'inner sync error' ) ; | ||
} ) | ||
.catch( function( error ) { | ||
expect( error.message ).to.be( 'inner sync error' ) ; | ||
throw new Error( 're-throw sync error' ) ; | ||
} ) ; | ||
} ) | ||
.catch( function( error ) { | ||
expect( error.message ).to.be( 're-throw sync error' ) ; | ||
} ) ; | ||
} ) ; | ||
} ) ; | ||
describe( "setTimeout() and friends" , function() { | ||
it( "Async: setTimeout" , function( done ) { | ||
@@ -73,3 +97,3 @@ | ||
it( "Async: double setTimeout" , function( done ) { | ||
it( "Async: nested setTimeout" , function( done ) { | ||
@@ -89,3 +113,3 @@ asyncTry( function() { | ||
it( "Async: quintuple setTimeout" , function( done ) { | ||
it( "Async: five nested setTimeout" , function( done ) { | ||
@@ -111,3 +135,3 @@ asyncTry( function() { | ||
it( "Async: double setTimeout, double async try catch, throw from the inner catch should bubble up to the outer catch" , function( done ) { | ||
it( "Async: nested setTimeout and async try catch, it should throw from the inner try, re-throw from the inner catch, bubble up to the outer catch" , function( done ) { | ||
@@ -127,3 +151,3 @@ asyncTry( function outerTry() { | ||
//console.log( 'inner' , AsyncTryCatch.stack , "\n" ) ; | ||
throw new Error( 'outer setTimeout error' ) ; | ||
throw new Error( 're-throw setTimeout error' ) ; | ||
} ) ; | ||
@@ -133,3 +157,3 @@ } , 0 ) ; | ||
.catch( function outerCatch( error ) { | ||
expect( error.message ).to.be( 'outer setTimeout error' ) ; | ||
expect( error.message ).to.be( 're-throw setTimeout error' ) ; | ||
//console.log( 'outer' , AsyncTryCatch.stack ) ; | ||
@@ -142,1 +166,58 @@ done() ; | ||
describe( "Events" , function() { | ||
it( "an exception thrown from a listener within an async-try closure should be catched" , function( done ) { | ||
var emitter = Object.create( Events.prototype ) ; | ||
asyncTry( function() { | ||
emitter.on( 'damage' , function() { throw new Error( 'argh!' ) ; } ) ; | ||
} ) | ||
.catch( function( error ) { | ||
expect( error.message ).to.be( 'argh!' ) ; | ||
done() ; | ||
} ) ; | ||
emitter.emit( 'damage' ) ; | ||
} ) ; | ||
it( "an exception thrown from a listener, whose emit is within an async-try closure should be catched if everything is synchronous" , function( done ) { | ||
// works because emit is sync here | ||
var emitter = Object.create( Events.prototype ) ; | ||
emitter.on( 'damage' , function() { throw new Error( 'argh!' ) ; } ) ; | ||
asyncTry( function() { | ||
emitter.emit( 'damage' ) ; | ||
} ) | ||
.catch( function( error ) { | ||
expect( error.message ).to.be( 'argh!' ) ; | ||
done() ; | ||
} ) ; | ||
} ) ; | ||
} ) ; | ||
describe( "NextGen Events" , function() { | ||
it( "an exception thrown from a listener within an async-try closure should be catched" , function( done ) { | ||
var emitter = Object.create( NextGenEvents.prototype ) ; | ||
asyncTry( function() { | ||
emitter.on( 'damage' , function() { throw new Error( 'argh!' ) ; } ) ; | ||
} ) | ||
.catch( function( error ) { | ||
expect( error.message ).to.be( 'argh!' ) ; | ||
done() ; | ||
} ) ; | ||
emitter.emit( 'damage' ) ; | ||
} ) ; | ||
} ) ; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21980
4
327