variable = null
try
{
print ("Level 0 func")
try
{
print ("Level 1 func")
throw "myerror"
}
catch (error)
{
print ("Level 1 onerror:" + error)
throw "newerror"
}
}
catch (error)
{
print ("Level 0 onerror:" + error)
variable = "Prm"
}
print ("Level 0 func2:" + variable)
add (// Level 0
func (as) {
print ("Level 0 func")
add (// Level 1
func (as) {
print ("Level 1 func")
as.error ("myerror")
},
onerror (as, error) {
print ("Level 1 onerror:" + error)
as.error ("newerror")
}
)
},
onerror (as, error) {
print ("Level 0 onerror:" + error)
as.success ("Prm")
}
)
add (// Level 0
func (as, param) {
print ("Level 0 func2:" + param)
as.success ()
}
)
Level 0 func
Level 1 func
Level 1 onerror: myerror
Level 0 onerror: newerror
Level 0 func2: Prm
// CommonJS . browser' $as var async_steps = require('futoin-asyncsteps'); // -, var root_as = async_steps(); // root_as.add( function( as ){ // as.success( "MyValue" ); } ) // .add( // , try function( as, arg ){ if ( arg === 'MyValue' ) // true { // as.add( function( as ){ // MyError as.error( 'MyError', 'Something bad has happened' ); }); } }, // - , catch function( as, err ) { if ( err === 'MyError' ) // true { // , as.success( 'NotSoBad' ); } } ) .add( function( as, arg ) { if ( arg === 'NotSoBad' ) { // as.state.error_info console.log( 'MyError was ignored: ' + as.state.error_info ); } // , as.state.p1arg = 'abc'; as.state.p2arg = 'xyz'; // , p, . // , var p = as.parallel(); p.add( function( as ){ console.log( 'Parallel Step 1' ); as.add( function( as ){ console.log( 'Parallel Step 1.1' ); as.state.p1 = as.state.p1arg + '1'; // as.success() } ); } ) .add( function( as ){ console.log( 'Parallel Step 2' ); as.add( function( as ){ console.log( 'Parallel Step 2.1' ); as.state.p2 = as.state.p2arg + '2'; } ); } ); } ) .add( function( as ){ console.log( 'Parallel 1 result: ' + as.state.p1 ); console.log( 'Parallel 2 result: ' + as.state.p2 ); } ); // , " " root_as.execute(); MyError was ignored: Something bad has happened Parallel Step 1 Parallel Step 2 Parallel Step 1.1 Parallel Step 2.1 Parallel 1 result: abc1 Parallel 2 result: xyz2
// browser $as().add( function( as ){ as.repeat( 3, function( as, i ) { console.log( "> Repeat: " + i ); } ); as.forEach( [ 1, 2, 3 ], function( as, k, v ) { console.log( "> forEach: " + k + " = " + v ); } ); as.forEach( { a: 1, b: 2, c: 3 }, function( as, k, v ) { console.log( "> forEach: " + k + " = " + v ); } ); } ) .loop( function( as ){ call_some_library( as ); as.add( func( as, result ){ if ( !result ) { // exit loop as.break(); } } ); } ) .execute(); > Repeat: 0 > Repeat: 1 > Repeat: 2 > forEach: 0 = 1 > forEach: 1 = 2 > forEach: 2 = 3 > forEach: a = 1 > forEach: b = 2 > forEach: c = 3
function dummy_service_read( success, error ){ // success() // error() } function dummy_service_cancel( reqhandle ){ // dummy_service_read() } var as = async_steps(); as.add( function( as ){ setImmediate( function(){ as.success( 'async success()' ); } ); as.setTimeout( 10 ); // ms // as.success() - setTimeout() } ).add( function( as, arg ){ console.log( arg ); var reqhandle = dummy_service_read( function( data ){ as.success( data ); }, function( err ){ if ( err !== 'SomeSpecificCancelCode' ) { try { as.error( err ); } catch ( e ) { // - - } } } ); as.setCancel(function(as){ dummy_service_cancel( reqhandle ); }); // as.success() - setCancel() // OPTIONAL. 1 as.setTimeout( 1000 ); }, function( as, err ) { console.log( err + ": " + as.state.error_info ); } ).execute(); setTimeout( function(){ // as.cancel(); }, 100 ); .add( function( as, arg ){ ... }, function( as, err ) { console.log( err + ": " + as.state.error_info ); console.log( as.state.last_exception.stack ); } ) async_steps.installAsyncToolTest(); var as = async_steps(); as.state.second_called = false; as.add( function( as ){ as.success(); }, function( as, error ){ error.should.equal( "Does not work" ); } ).add( function( as ){ as.state.second_called = true; as.success(); } ); as.execute(); as.state.second_called.should.be.false; async_steps.AsyncTool.getEvents().length.should.be.above( 0 ); async_steps.AsyncTool.nextEvent(); as.state.second_called.should.be.true; async_steps.AsyncTool.getEvents().length.should.equal( 0 ); Source: https://habr.com/ru/post/249087/
All Articles