control-structures
a tiny library aids for using asynchronous functions with various control structures. [GitHub Repository] [npm registry]
Installation
> npm install control-structures
how to use
you can use this library with browser contexts or with Node.
With browser contexts
only you should do is to include a minified script.
like this.
Async testtest <!--scripts-->
With node
you can use this library with a 'require' statement.
var cs = ;
functions
this tiny library supports basic 6 types of control structure for asynchronous functions.
simple continuation
simple continuation ('cs._') realize sequential execution of comma separated blocks, and pass arguments from a 'next' function to the next function.
= consolelog str; setTimeoutnext200 cs_ 'arg1''arg2' PRINT '[1st block]'-> PRINT 'str1:'+str1-> PRINT 'str2:'+str2-> next 'err' PRINT '[2nd block]'-> PRINT 'errstr:'+errstr-> next PRINT '[last block]'-> next
join after if, if-else or switch
simple continuation ('cs._') also realize joining after if, if-else or switch.
= consolelog str; setTimeoutnext200 cs_ 'arg1''arg2' PRINT '[1st block]'-> if str1 == str2 PRINT 'str1 and str2:'+str1-> next else PRINT 'str1:'+str1-> PRINT 'str2:'+str2-> next PRINT '[2nd block]'-> next
simultaneous each (alike 'each')
you can apply same function to each member of 'Array' or 'Object'.
= consolelog str; setTimeoutnext200 cs_each 235711'arg1' PRINT 'apply'-> nextvali for num in array_of_args_from_next1 consolelog 'value: '+numtoString for num in array_of_args_from_next2 consolelog 'counter: '+numtoString next
loop with counter (alike 'for')
loop with counter ('cs._for') realize loop
= consolelog str; setTimeoutnext200 cs_for 0 n<10 n+1 PRINT 'counter: '+ntoString-> _next PRINT 'last counter: '+ntoString-> return
loop around an array or an object (alike 'for {in,of}')
= consolelog str; setTimeoutnext200 cs_for_in bro:'ani'sis:'imo'dad:'tousan'mom:'kaasan'#first args PRINT key+': '+val-> _next -> PRINT 'end'-> return
simple loop (alike 'while')
= consolelog str; setTimeoutnext200 cs_while 25*2525*88 PRINT arg1toString-> PRINT arg2toString-> if arg2 % arg1 == 0 _breakarg1 else _nextarg2 % arg1arg1 PRINT 'result: '+argtoString-> return
exception handling (alike 'try, throw, catch, finally')
= consolelog str; setTimeoutnext200 myexc = myexc_try-> #block PRINT 'NEST1-1'-> PRINT 'NEST1-2'-> myexc_try -> #block PRINT 'NEST2-1'-> PRINT 'NEST2-2'-> if true myexc_throw 'err1' else PRINT 'NEST2-3'-> myexc_finally 'err2''err3' #catch consolelog _e PRINT 'NEST2-CATCH'-> myexc_finally #finally PRINT 'NEST2-FINALLY'-> fnext -> PRINT 'NEST1-3'-> myexc_finally 'err1' #catch consolelog _e PRINT 'NEST1-CATCH'-> myexc_finally #finally PRINT 'NEST1-FINALLY' fnextnext
y combinator
you can make an recursive function from anonymous one.
dummy = nullindex = 10csy return index -= 1 A -> PRINT indextoString-> if index == 0 return next else funcdummydummy