async.js 225KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078
  1. (function (global, factory) {
  2. /*jshint -W030 */
  3. 'use strict';
  4. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  5. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  6. global.async ? factory(global.neo_async = global.neo_async || {}) :
  7. factory(global.async = global.async || {});
  8. }(this, function(exports) {
  9. 'use strict';
  10. var noop = function noop() {};
  11. var throwError = function throwError() {
  12. throw new Error('Callback was already called.');
  13. };
  14. var DEFAULT_TIMES = 5;
  15. var DEFAULT_INTERVAL = 0;
  16. var obj = 'object';
  17. var func = 'function';
  18. var isArray = Array.isArray;
  19. var nativeKeys = Object.keys;
  20. var nativePush = Array.prototype.push;
  21. var iteratorSymbol = typeof Symbol === func && Symbol.iterator;
  22. var nextTick, asyncNextTick, asyncSetImmediate;
  23. createImmediate();
  24. /**
  25. * @memberof async
  26. * @namespace each
  27. * @param {Array|Object} collection
  28. * @param {Function} iterator
  29. * @param {Function} callback
  30. * @example
  31. *
  32. * // array
  33. * var order = [];
  34. * var array = [1, 3, 2];
  35. * var iterator = function(num, done) {
  36. * setTimeout(function() {
  37. * order.push(num);
  38. * done();
  39. * }, num * 10);
  40. * };
  41. * async.each(array, iterator, function(err, res) {
  42. * console.log(res); // undefined
  43. * console.log(order); // [1, 2, 3]
  44. * });
  45. *
  46. * @example
  47. *
  48. * // array with index
  49. * var order = [];
  50. * var array = [1, 3, 2];
  51. * var iterator = function(num, index, done) {
  52. * setTimeout(function() {
  53. * order.push([num, index]);
  54. * done();
  55. * }, num * 10);
  56. * };
  57. * async.each(array, iterator, function(err, res) {
  58. * console.log(res); // undefined
  59. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  60. * });
  61. *
  62. * @example
  63. *
  64. * // object
  65. * var order = [];
  66. * var object = { a: 1, b: 3, c: 2 };
  67. * var iterator = function(num, done) {
  68. * setTimeout(function() {
  69. * order.push(num);
  70. * done();
  71. * }, num * 10);
  72. * };
  73. * async.each(object, iterator, function(err, res) {
  74. * console.log(res); // undefined
  75. * console.log(order); // [1, 2, 3]
  76. * });
  77. *
  78. * @example
  79. *
  80. * // object with key
  81. * var order = [];
  82. * var object = { a: 1, b: 3, c: 2 };
  83. * var iterator = function(num, key, done) {
  84. * setTimeout(function() {
  85. * order.push([num, key]);
  86. * done();
  87. * }, num * 10);
  88. * };
  89. * async.each(object, iterator, function(err, res) {
  90. * console.log(res); // undefined
  91. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  92. * });
  93. *
  94. * @example
  95. *
  96. * // break
  97. * var order = [];
  98. * var array = [1, 3, 2];
  99. * var iterator = function(num, done) {
  100. * setTimeout(function() {
  101. * order.push(num);
  102. * done(null, num !== 2);
  103. * }, num * 10);
  104. * };
  105. * async.each(array, iterator, function(err, res) {
  106. * console.log(res); // undefined
  107. * console.log(order); // [1, 2]
  108. * });
  109. *
  110. */
  111. var each = createEach(arrayEach, baseEach, symbolEach);
  112. /**
  113. * @memberof async
  114. * @namespace map
  115. * @param {Array|Object} collection
  116. * @param {Function} iterator
  117. * @param {Function} callback
  118. * @example
  119. *
  120. * // array
  121. * var order = [];
  122. * var array = [1, 3, 2];
  123. * var iterator = function(num, done) {
  124. * setTimeout(function() {
  125. * order.push(num);
  126. * done(null, num);
  127. * }, num * 10);
  128. * };
  129. * async.map(array, iterator, function(err, res) {
  130. * console.log(res); // [1, 3, 2];
  131. * console.log(order); // [1, 2, 3]
  132. * });
  133. *
  134. * @example
  135. *
  136. * // array with index
  137. * var order = [];
  138. * var array = [1, 3, 2];
  139. * var iterator = function(num, index, done) {
  140. * setTimeout(function() {
  141. * order.push([num, index]);
  142. * done(null, num);
  143. * }, num * 10);
  144. * };
  145. * async.map(array, iterator, function(err, res) {
  146. * console.log(res); // [1, 3, 2]
  147. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  148. * });
  149. *
  150. * @example
  151. *
  152. * // object
  153. * var order = [];
  154. * var object = { a: 1, b: 3, c: 2 };
  155. * var iterator = function(num, done) {
  156. * setTimeout(function() {
  157. * order.push(num);
  158. * done(null, num);
  159. * }, num * 10);
  160. * };
  161. * async.map(object, iterator, function(err, res) {
  162. * console.log(res); // [1, 3, 2]
  163. * console.log(order); // [1, 2, 3]
  164. * });
  165. *
  166. * @example
  167. *
  168. * // object with key
  169. * var order = [];
  170. * var object = { a: 1, b: 3, c: 2 };
  171. * var iterator = function(num, key, done) {
  172. * setTimeout(function() {
  173. * order.push([num, key]);
  174. * done(null, num);
  175. * }, num * 10);
  176. * };
  177. * async.map(object, iterator, function(err, res) {
  178. * console.log(res); // [1, 3, 2]
  179. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  180. * });
  181. *
  182. */
  183. var map = createMap(arrayEachIndex, baseEachIndex, symbolEachIndex, true);
  184. /**
  185. * @memberof async
  186. * @namespace mapValues
  187. * @param {Array|Object} collection
  188. * @param {Function} iterator
  189. * @param {Function} callback
  190. * @example
  191. *
  192. * // array
  193. * var order = [];
  194. * var array = [1, 3, 2];
  195. * var iterator = function(num, done) {
  196. * setTimeout(function() {
  197. * order.push(num);
  198. * done(null, num);
  199. * }, num * 10);
  200. * };
  201. * async.mapValues(array, iterator, function(err, res) {
  202. * console.log(res); // { '0': 1, '1': 3, '2': 2 }
  203. * console.log(order); // [1, 2, 3]
  204. * });
  205. *
  206. * @example
  207. *
  208. * // array with index
  209. * var order = [];
  210. * var array = [1, 3, 2];
  211. * var iterator = function(num, index, done) {
  212. * setTimeout(function() {
  213. * order.push([num, index]);
  214. * done(null, num);
  215. * }, num * 10);
  216. * };
  217. * async.mapValues(array, iterator, function(err, res) {
  218. * console.log(res); // { '0': 1, '1': 3, '2': 2 }
  219. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  220. * });
  221. *
  222. * @example
  223. *
  224. * // object
  225. * var order = [];
  226. * var object = { a: 1, b: 3, c: 2 };
  227. * var iterator = function(num, done) {
  228. * setTimeout(function() {
  229. * order.push(num);
  230. * done(null, num);
  231. * }, num * 10);
  232. * };
  233. * async.mapValues(object, iterator, function(err, res) {
  234. * console.log(res); // { a: 1, b: 3, c: 2 }
  235. * console.log(order); // [1, 2, 3]
  236. * });
  237. *
  238. * @example
  239. *
  240. * // object with key
  241. * var order = [];
  242. * var object = { a: 1, b: 3, c: 2 };
  243. * var iterator = function(num, key, done) {
  244. * setTimeout(function() {
  245. * order.push([num, key]);
  246. * done(null, num);
  247. * }, num * 10);
  248. * };
  249. * async.mapValues(object, iterator, function(err, res) {
  250. * console.log(res); // { a: 1, b: 3, c: 2 }
  251. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  252. * });
  253. *
  254. */
  255. var mapValues = createMap(arrayEachIndex, baseEachKey, symbolEachKey, false);
  256. /**
  257. * @memberof async
  258. * @namespace filter
  259. * @param {Array|Object} collection
  260. * @param {Function} iterator
  261. * @param {Function} callback
  262. * @example
  263. *
  264. * // array
  265. * var order = [];
  266. * var array = [1, 3, 2];
  267. * var iterator = function(num, done) {
  268. * setTimeout(function() {
  269. * order.push(num);
  270. * done(null, num % 2);
  271. * }, num * 10);
  272. * };
  273. * async.filter(array, iterator, function(err, res) {
  274. * console.log(res); // [1, 3];
  275. * console.log(order); // [1, 2, 3]
  276. * });
  277. *
  278. * @example
  279. *
  280. * // array with index
  281. * var order = [];
  282. * var array = [1, 3, 2];
  283. * var iterator = function(num, index, done) {
  284. * setTimeout(function() {
  285. * order.push([num, index]);
  286. * done(null, num % 2);
  287. * }, num * 10);
  288. * };
  289. * async.filter(array, iterator, function(err, res) {
  290. * console.log(res); // [1, 3];
  291. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  292. * });
  293. *
  294. * @example
  295. *
  296. * // object
  297. * var order = [];
  298. * var object = { a: 1, b: 3, c: 2 };
  299. * var iterator = function(num, done) {
  300. * setTimeout(function() {
  301. * order.push(num);
  302. * done(null, num % 2);
  303. * }, num * 10);
  304. * };
  305. * async.filter(object, iterator, function(err, res) {
  306. * console.log(res); // [1, 3];
  307. * console.log(order); // [1, 2, 3]
  308. * });
  309. *
  310. * @example
  311. *
  312. * // object with key
  313. * var order = [];
  314. * var object = { a: 1, b: 3, c: 2 };
  315. * var iterator = function(num, key, done) {
  316. * setTimeout(function() {
  317. * order.push([num, key]);
  318. * done(null, num % 2);
  319. * }, num * 10);
  320. * };
  321. * async.filter(object, iterator, function(err, res) {
  322. * console.log(res); // [1, 3];
  323. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  324. * });
  325. *
  326. */
  327. var filter = createFilter(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue, true);
  328. /**
  329. * @memberof async
  330. * @namespace filterSeries
  331. * @param {Array|Object} collection
  332. * @param {Function} iterator
  333. * @param {Function} callback
  334. * @example
  335. *
  336. * // array
  337. * var order = [];
  338. * var array = [1, 3, 2];
  339. * var iterator = function(num, done) {
  340. * setTimeout(function() {
  341. * order.push(num);
  342. * done(null, num % 2);
  343. * }, num * 10);
  344. * };
  345. * async.filterSeries(array, iterator, function(err, res) {
  346. * console.log(res); // [1, 3];
  347. * console.log(order); // [1, 3, 2]
  348. * });
  349. *
  350. * @example
  351. *
  352. * // array with index
  353. * var order = [];
  354. * var array = [1, 3, 2];
  355. * var iterator = function(num, index, done) {
  356. * setTimeout(function() {
  357. * order.push([num, index]);
  358. * done(null, num % 2);
  359. * }, num * 10);
  360. * };
  361. * async.filterSeries(array, iterator, function(err, res) {
  362. * console.log(res); // [1, 3]
  363. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  364. * });
  365. *
  366. * @example
  367. *
  368. * // object
  369. * var order = [];
  370. * var object = { a: 1, b: 3, c: 2 };
  371. * var iterator = function(num, done) {
  372. * setTimeout(function() {
  373. * order.push(num);
  374. * done(null, num % 2);
  375. * }, num * 10);
  376. * };
  377. * async.filterSeries(object, iterator, function(err, res) {
  378. * console.log(res); // [1, 3]
  379. * console.log(order); // [1, 3, 2]
  380. * });
  381. *
  382. * @example
  383. *
  384. * // object with key
  385. * var order = [];
  386. * var object = { a: 1, b: 3, c: 2 };
  387. * var iterator = function(num, key, done) {
  388. * setTimeout(function() {
  389. * order.push([num, key]);
  390. * done(null, num % 2);
  391. * }, num * 10);
  392. * };
  393. * async.filterSeries(object, iterator, function(err, res) {
  394. * console.log(res); // [1, 3]
  395. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
  396. * });
  397. *
  398. */
  399. var filterSeries = createFilterSeries(true);
  400. /**
  401. * @memberof async
  402. * @namespace filterLimit
  403. * @param {Array|Object} collection
  404. * @param {number} limit - limit >= 1
  405. * @param {Function} iterator
  406. * @param {Function} callback
  407. * @example
  408. *
  409. * // array
  410. * var order = [];
  411. * var array = [1, 5, 3, 4, 2];
  412. * var iterator = function(num, done) {
  413. * setTimeout(function() {
  414. * order.push(num);
  415. * done(null, num % 2);
  416. * }, num * 10);
  417. * };
  418. * async.filterLimit(array, 2, iterator, function(err, res) {
  419. * console.log(res); // [1, 5, 3]
  420. * console.log(order); // [1, 3, 5, 2, 4]
  421. * });
  422. *
  423. * @example
  424. *
  425. * // array with index
  426. * var order = [];
  427. * var array = [1, 5, 3, 4, 2];
  428. * var iterator = function(num, index, done) {
  429. * setTimeout(function() {
  430. * order.push([num, index]);
  431. * done(null, num % 2);
  432. * }, num * 10);
  433. * };
  434. * async.filterLimit(array, 2, iterator, function(err, res) {
  435. * console.log(res); // [1, 5, 3]
  436. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  437. * });
  438. *
  439. * @example
  440. *
  441. * // object
  442. * var order = [];
  443. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  444. * var iterator = function(num, done) {
  445. * setTimeout(function() {
  446. * order.push(num);
  447. * done(null, num % 2);
  448. * }, num * 10);
  449. * };
  450. * async.filterLimit(object, 2, iterator, function(err, res) {
  451. * console.log(res); // [1, 5, 3]
  452. * console.log(order); // [1, 3, 5, 2, 4]
  453. * });
  454. *
  455. * @example
  456. *
  457. * // object with key
  458. * var order = [];
  459. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  460. * var iterator = function(num, key, done) {
  461. * setTimeout(function() {
  462. * order.push([num, key]);
  463. * done(null, num % 2);
  464. * }, num * 10);
  465. * };
  466. * async.filterLimit(object, 2, iterator, function(err, res) {
  467. * console.log(res); // [1, 5, 3]
  468. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  469. * });
  470. *
  471. */
  472. var filterLimit = createFilterLimit(true);
  473. /**
  474. * @memberof async
  475. * @namespace reject
  476. * @param {Array|Object} collection
  477. * @param {Function} iterator
  478. * @param {Function} callback
  479. * @example
  480. *
  481. * // array
  482. * var order = [];
  483. * var array = [1, 3, 2];
  484. * var iterator = function(num, done) {
  485. * setTimeout(function() {
  486. * order.push(num);
  487. * done(null, num % 2);
  488. * }, num * 10);
  489. * };
  490. * async.reject(array, iterator, function(err, res) {
  491. * console.log(res); // [2];
  492. * console.log(order); // [1, 2, 3]
  493. * });
  494. *
  495. * @example
  496. *
  497. * // array with index
  498. * var order = [];
  499. * var array = [1, 3, 2];
  500. * var iterator = function(num, index, done) {
  501. * setTimeout(function() {
  502. * order.push([num, index]);
  503. * done(null, num % 2);
  504. * }, num * 10);
  505. * };
  506. * async.reject(array, iterator, function(err, res) {
  507. * console.log(res); // [2];
  508. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  509. * });
  510. *
  511. * @example
  512. *
  513. * // object
  514. * var order = [];
  515. * var object = { a: 1, b: 3, c: 2 };
  516. * var iterator = function(num, done) {
  517. * setTimeout(function() {
  518. * order.push(num);
  519. * done(null, num % 2);
  520. * }, num * 10);
  521. * };
  522. * async.reject(object, iterator, function(err, res) {
  523. * console.log(res); // [2];
  524. * console.log(order); // [1, 2, 3]
  525. * });
  526. *
  527. * @example
  528. *
  529. * // object with key
  530. * var order = [];
  531. * var object = { a: 1, b: 3, c: 2 };
  532. * var iterator = function(num, key, done) {
  533. * setTimeout(function() {
  534. * order.push([num, key]);
  535. * done(null, num % 2);
  536. * }, num * 10);
  537. * };
  538. * async.reject(object, iterator, function(err, res) {
  539. * console.log(res); // [2];
  540. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  541. * });
  542. *
  543. */
  544. var reject = createFilter(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue, false);
  545. /**
  546. * @memberof async
  547. * @namespace rejectSeries
  548. * @param {Array|Object} collection
  549. * @param {Function} iterator
  550. * @param {Function} callback
  551. * @example
  552. *
  553. * // array
  554. * var order = [];
  555. * var array = [1, 3, 2];
  556. * var iterator = function(num, done) {
  557. * setTimeout(function() {
  558. * order.push(num);
  559. * done(null, num % 2);
  560. * }, num * 10);
  561. * };
  562. * async.rejectSeries(array, iterator, function(err, res) {
  563. * console.log(res); // [2];
  564. * console.log(order); // [1, 3, 2]
  565. * });
  566. *
  567. * @example
  568. *
  569. * // object
  570. * var order = [];
  571. * var object = { a: 1, b: 3, c: 2 };
  572. * var iterator = function(num, done) {
  573. * setTimeout(function() {
  574. * order.push(num);
  575. * done(null, num % 2);
  576. * }, num * 10);
  577. * };
  578. * async.rejectSeries(object, iterator, function(err, res) {
  579. * console.log(res); // [2];
  580. * console.log(order); // [1, 3, 2]
  581. * });
  582. *
  583. * @example
  584. *
  585. * // object with key
  586. * var order = [];
  587. * var object = { a: 1, b: 3, c: 2 };
  588. * var iterator = function(num, key, done) {
  589. * setTimeout(function() {
  590. * order.push([num, key]);
  591. * done(null, num % 2);
  592. * }, num * 10);
  593. * };
  594. * async.rejectSeries(object, iterator, function(err, res) {
  595. * console.log(res); // [2];
  596. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
  597. * });
  598. *
  599. */
  600. var rejectSeries = createFilterSeries(false);
  601. /**
  602. * @memberof async
  603. * @namespace rejectLimit
  604. * @param {Array|Object} collection
  605. * @param {number} limit - limit >= 1
  606. * @param {Function} iterator
  607. * @param {Function} callback
  608. * @example
  609. *
  610. * // array
  611. * var order = [];
  612. * var array = [1, 5, 3, 4, 2];
  613. * var iterator = function(num, done) {
  614. * setTimeout(function() {
  615. * order.push(num);
  616. * done(null, num % 2);
  617. * }, num * 10);
  618. * };
  619. * async.rejectLimit(array, 2, iterator, function(err, res) {
  620. * console.log(res); // [4, 2]
  621. * console.log(order); // [1, 3, 5, 2, 4]
  622. * });
  623. *
  624. * @example
  625. *
  626. * // array with index
  627. * var order = [];
  628. * var array = [1, 5, 3, 4, 2];
  629. * var iterator = function(num, index, done) {
  630. * setTimeout(function() {
  631. * order.push([num, index]);
  632. * done(null, num % 2);
  633. * }, num * 10);
  634. * };
  635. * async.rejectLimit(array, 2, iterator, function(err, res) {
  636. * console.log(res); // [4, 2]
  637. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  638. * });
  639. *
  640. * @example
  641. *
  642. * // object
  643. * var order = [];
  644. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  645. * var iterator = function(num, done) {
  646. * setTimeout(function() {
  647. * order.push(num);
  648. * done(null, num % 2);
  649. * }, num * 10);
  650. * };
  651. * async.rejectLimit(object, 2, iterator, function(err, res) {
  652. * console.log(res); // [4, 2]
  653. * console.log(order); // [1, 3, 5, 2, 4]
  654. * });
  655. *
  656. * @example
  657. *
  658. * // object with key
  659. * var order = [];
  660. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  661. * var iterator = function(num, key, done) {
  662. * setTimeout(function() {
  663. * order.push([num, key]);
  664. * done(null, num % 2);
  665. * }, num * 10);
  666. * };
  667. * async.rejectLimit(object, 2, iterator, function(err, res) {
  668. * console.log(res); // [4, 2]
  669. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  670. * });
  671. *
  672. */
  673. var rejectLimit = createFilterLimit(false);
  674. /**
  675. * @memberof async
  676. * @namespace detect
  677. * @param {Array|Object} collection
  678. * @param {Function} iterator
  679. * @param {Function} callback
  680. * @example
  681. *
  682. * // array
  683. * var order = [];
  684. * var array = [1, 3, 2];
  685. * var iterator = function(num, done) {
  686. * setTimeout(function() {
  687. * order.push(num);
  688. * done(null, num % 2);
  689. * }, num * 10);
  690. * };
  691. * async.detect(array, iterator, function(err, res) {
  692. * console.log(res); // 1
  693. * console.log(order); // [1]
  694. * });
  695. *
  696. * @example
  697. *
  698. * // array with index
  699. * var order = [];
  700. * var array = [1, 3, 2];
  701. * var iterator = function(num, index, done) {
  702. * setTimeout(function() {
  703. * order.push([num, index]);
  704. * done(null, num % 2);
  705. * }, num * 10);
  706. * };
  707. * async.detect(array, iterator, function(err, res) {
  708. * console.log(res); // 1
  709. * console.log(order); // [[1, 0]]
  710. * });
  711. *
  712. * @example
  713. *
  714. * // object
  715. * var order = [];
  716. * var object = { a: 1, b: 3, c: 2 };
  717. * var iterator = function(num, done) {
  718. * setTimeout(function() {
  719. * order.push(num);
  720. * done(null, num % 2);
  721. * }, num * 10);
  722. * };
  723. * async.detect(object, iterator, function(err, res) {
  724. * console.log(res); // 1
  725. * console.log(order); // [1]
  726. * });
  727. *
  728. * @example
  729. *
  730. * // object with key
  731. * var order = [];
  732. * var object = { a: 1, b: 3, c: 2 };
  733. * var iterator = function(num, key, done) {
  734. * setTimeout(function() {
  735. * order.push([num, key]);
  736. * done(null, num % 2);
  737. * }, num * 10);
  738. * };
  739. * async.detect(object, iterator, function(err, res) {
  740. * console.log(res); // 1
  741. * console.log(order); // [[1, 'a']]
  742. * });
  743. *
  744. */
  745. var detect = createDetect(arrayEachValue, baseEachValue, symbolEachValue, true);
  746. /**
  747. * @memberof async
  748. * @namespace detectSeries
  749. * @param {Array|Object} collection
  750. * @param {Function} iterator
  751. * @param {Function} callback
  752. * @example
  753. *
  754. * // array
  755. * var order = [];
  756. * var array = [1, 3, 2];
  757. * var iterator = function(num, done) {
  758. * setTimeout(function() {
  759. * order.push(num);
  760. * done(null, num % 2);
  761. * }, num * 10);
  762. * };
  763. * async.detectSeries(array, iterator, function(err, res) {
  764. * console.log(res); // 1
  765. * console.log(order); // [1]
  766. * });
  767. *
  768. * @example
  769. *
  770. * // array with index
  771. * var order = [];
  772. * var array = [1, 3, 2];
  773. * var iterator = function(num, index, done) {
  774. * setTimeout(function() {
  775. * order.push([num, index]);
  776. * done(null, num % 2);
  777. * }, num * 10);
  778. * };
  779. * async.detectSeries(array, iterator, function(err, res) {
  780. * console.log(res); // 1
  781. * console.log(order); // [[1, 0]]
  782. * });
  783. *
  784. * @example
  785. *
  786. * // object
  787. * var order = [];
  788. * var object = { a: 1, b: 3, c: 2 };
  789. * var iterator = function(num, done) {
  790. * setTimeout(function() {
  791. * order.push(num);
  792. * done(null, num % 2);
  793. * }, num * 10);
  794. * };
  795. * async.detectSeries(object, iterator, function(err, res) {
  796. * console.log(res); // 1
  797. * console.log(order); // [1]
  798. * });
  799. *
  800. * @example
  801. *
  802. * // object with key
  803. * var order = [];
  804. * var object = { a: 1, b: 3, c: 2 };
  805. * var iterator = function(num, key, done) {
  806. * setTimeout(function() {
  807. * order.push([num, key]);
  808. * done(null, num % 2);
  809. * }, num * 10);
  810. * };
  811. * async.detectSeries(object, iterator, function(err, res) {
  812. * console.log(res); // 1
  813. * console.log(order); // [[1, 'a']]
  814. * });
  815. *
  816. */
  817. var detectSeries = createDetectSeries(true);
  818. /**
  819. * @memberof async
  820. * @namespace detectLimit
  821. * @param {Array|Object} collection
  822. * @param {number} limit - limit >= 1
  823. * @param {Function} iterator
  824. * @param {Function} callback
  825. * @example
  826. *
  827. * // array
  828. * var order = [];
  829. * var array = [1, 5, 3, 4, 2];
  830. * var iterator = function(num, done) {
  831. * setTimeout(function() {
  832. * order.push(num);
  833. * done(null, num % 2);
  834. * }, num * 10);
  835. * };
  836. * async.detectLimit(array, 2, iterator, function(err, res) {
  837. * console.log(res); // 1
  838. * console.log(order); // [1]
  839. * });
  840. *
  841. * @example
  842. *
  843. * // array with index
  844. * var order = [];
  845. * var array = [1, 5, 3, 4, 2];
  846. * var iterator = function(num, index, done) {
  847. * setTimeout(function() {
  848. * order.push([num, index]);
  849. * done(null, num % 2);
  850. * }, num * 10);
  851. * };
  852. * async.detectLimit(array, 2, iterator, function(err, res) {
  853. * console.log(res); // 1
  854. * console.log(order); // [[1, 0]]
  855. * });
  856. *
  857. * @example
  858. *
  859. * // object
  860. * var order = [];
  861. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  862. * var iterator = function(num, done) {
  863. * setTimeout(function() {
  864. * order.push(num);
  865. * done(null, num % 2);
  866. * }, num * 10);
  867. * };
  868. * async.detectLimit(object, 2, iterator, function(err, res) {
  869. * console.log(res); // 1
  870. * console.log(order); // [1]
  871. * });
  872. *
  873. * @example
  874. *
  875. * // object with key
  876. * var order = [];
  877. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  878. * var iterator = function(num, key, done) {
  879. * setTimeout(function() {
  880. * order.push([num, key]);
  881. * done(null, num % 2);
  882. * }, num * 10);
  883. * };
  884. * async.detectLimit(object, 2, iterator, function(err, res) {
  885. * console.log(res); // 1
  886. * console.log(order); // [[1, 'a']]
  887. * });
  888. *
  889. */
  890. var detectLimit = createDetectLimit(true);
  891. /**
  892. * @memberof async
  893. * @namespace every
  894. * @param {Array|Object} collection
  895. * @param {Function} iterator
  896. * @param {Function} callback
  897. * @example
  898. *
  899. * // array
  900. * var order = [];
  901. * var array = [1, 3, 2];
  902. * var iterator = function(num, done) {
  903. * setTimeout(function() {
  904. * order.push(num);
  905. * done(null, num % 2);
  906. * }, num * 10);
  907. * };
  908. * async.every(array, iterator, function(err, res) {
  909. * console.log(res); // false
  910. * console.log(order); // [1, 2]
  911. * });
  912. *
  913. * @example
  914. *
  915. * // array with index
  916. * var order = [];
  917. * var array = [1, 3, 2];
  918. * var iterator = function(num, index, done) {
  919. * setTimeout(function() {
  920. * order.push([num, index]);
  921. * done(null, num % 2);
  922. * }, num * 10);
  923. * };
  924. * async.every(array, iterator, function(err, res) {
  925. * console.log(res); // false
  926. * console.log(order); // [[1, 0], [2, 2]]
  927. * });
  928. *
  929. * @example
  930. *
  931. * // object
  932. * var order = [];
  933. * var object = { a: 1, b: 3, c: 2 };
  934. * var iterator = function(num, done) {
  935. * setTimeout(function() {
  936. * order.push(num);
  937. * done(null, num % 2);
  938. * }, num * 10);
  939. * };
  940. * async.every(object, iterator, function(err, res) {
  941. * console.log(res); // false
  942. * console.log(order); // [1, 2]
  943. * });
  944. *
  945. * @example
  946. *
  947. * // object with key
  948. * var order = [];
  949. * var object = { a: 1, b: 3, c: 2 };
  950. * var iterator = function(num, key, done) {
  951. * setTimeout(function() {
  952. * order.push([num, key]);
  953. * done(null, num % 2);
  954. * }, num * 10);
  955. * };
  956. * async.every(object, iterator, function(err, res) {
  957. * console.log(res); // false
  958. * console.log(order); // [[1, 'a'], [2, 'c']]
  959. * });
  960. *
  961. */
  962. var every = createEvery(arrayEachValue, baseEachValue, symbolEachValue);
  963. /**
  964. * @memberof async
  965. * @namespace everySeries
  966. * @param {Array|Object} collection
  967. * @param {Function} iterator
  968. * @param {Function} callback
  969. * @example
  970. *
  971. * // array
  972. * var order = [];
  973. * var array = [1, 3, 2];
  974. * var iterator = function(num, done) {
  975. * setTimeout(function() {
  976. * order.push(num);
  977. * done(null, num % 2);
  978. * }, num * 10);
  979. * };
  980. * async.everySeries(array, iterator, function(err, res) {
  981. * console.log(res); // false
  982. * console.log(order); // [1, 3, 2]
  983. * });
  984. *
  985. * @example
  986. *
  987. * // array with index
  988. * var order = [];
  989. * var array = [1, 3, 2];
  990. * var iterator = function(num, index, done) {
  991. * setTimeout(function() {
  992. * order.push([num, index]);
  993. * done(null, num % 2);
  994. * }, num * 10);
  995. * };
  996. * async.everySeries(array, iterator, function(err, res) {
  997. * console.log(res); // false
  998. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  999. * });
  1000. *
  1001. * @example
  1002. *
  1003. * // object
  1004. * var order = [];
  1005. * var object = { a: 1, b: 3, c: 2 };
  1006. * var iterator = function(num, done) {
  1007. * setTimeout(function() {
  1008. * order.push(num);
  1009. * done(null, num % 2);
  1010. * }, num * 10);
  1011. * };
  1012. * async.everySeries(object, iterator, function(err, res) {
  1013. * console.log(res); // false
  1014. * console.log(order); // [1, 3, 2]
  1015. * });
  1016. *
  1017. * @example
  1018. *
  1019. * // object with key
  1020. * var order = [];
  1021. * var object = { a: 1, b: 3, c: 2 };
  1022. * var iterator = function(num, key, done) {
  1023. * setTimeout(function() {
  1024. * order.push([num, key]);
  1025. * done(null, num % 2);
  1026. * }, num * 10);
  1027. * };
  1028. * async.everySeries(object, iterator, function(err, res) {
  1029. * console.log(res); // false
  1030. * console.log(order); // [[1, 'a'], [3, 'b'] [2, 'c']]
  1031. * });
  1032. *
  1033. */
  1034. var everySeries = createEverySeries();
  1035. /**
  1036. * @memberof async
  1037. * @namespace everyLimit
  1038. * @param {Array|Object} collection
  1039. * @param {number} limit - limit >= 1
  1040. * @param {Function} iterator
  1041. * @param {Function} callback
  1042. * @example
  1043. *
  1044. * // array
  1045. * var order = [];
  1046. * var array = [1, 5, 3, 4, 2];
  1047. * var iterator = function(num, done) {
  1048. * setTimeout(function() {
  1049. * order.push(num);
  1050. * done(null, num % 2);
  1051. * }, num * 10);
  1052. * };
  1053. * async.everyLimit(array, 2, iterator, function(err, res) {
  1054. * console.log(res); // false
  1055. * console.log(order); // [1, 3, 5, 2]
  1056. * });
  1057. *
  1058. * @example
  1059. *
  1060. * // array with index
  1061. * var order = [];
  1062. * var array = [1, 5, 3, 4, 2];
  1063. * var iterator = function(num, index, done) {
  1064. * setTimeout(function() {
  1065. * order.push([num, index]);
  1066. * done(null, num % 2);
  1067. * }, num * 10);
  1068. * };
  1069. * async.everyLimit(array, 2, iterator, function(err, res) {
  1070. * console.log(res); // false
  1071. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4]]
  1072. * });
  1073. *
  1074. * @example
  1075. *
  1076. * // object
  1077. * var order = [];
  1078. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1079. * var iterator = function(num, done) {
  1080. * setTimeout(function() {
  1081. * order.push(num);
  1082. * done(null, num % 2);
  1083. * }, num * 10);
  1084. * };
  1085. * async.everyLimit(object, 2, iterator, function(err, res) {
  1086. * console.log(res); // false
  1087. * console.log(order); // [1, 3, 5, 2]
  1088. * });
  1089. *
  1090. * @example
  1091. *
  1092. * // object with key
  1093. * var order = [];
  1094. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1095. * var iterator = function(num, key, done) {
  1096. * setTimeout(function() {
  1097. * order.push([num, key]);
  1098. * done(null, num % 2);
  1099. * }, num * 10);
  1100. * };
  1101. * async.everyLimit(object, 2, iterator, function(err, res) {
  1102. * console.log(res); // false
  1103. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e']]
  1104. * });
  1105. *
  1106. */
  1107. var everyLimit = createEveryLimit();
  1108. /**
  1109. * @memberof async
  1110. * @namespace pick
  1111. * @param {Array|Object} collection
  1112. * @param {Function} iterator
  1113. * @param {Function} callback
  1114. * @example
  1115. *
  1116. * // array
  1117. * var order = [];
  1118. * var array = [1, 3, 2, 4];
  1119. * var iterator = function(num, done) {
  1120. * setTimeout(function() {
  1121. * order.push(num);
  1122. * done(null, num % 2);
  1123. * }, num * 10);
  1124. * };
  1125. * async.pick(array, iterator, function(err, res) {
  1126. * console.log(res); // { '0': 1, '1': 3 }
  1127. * console.log(order); // [1, 2, 3, 4]
  1128. * });
  1129. *
  1130. * @example
  1131. *
  1132. * // array with index
  1133. * var order = [];
  1134. * var array = [1, 3, 2, 4];
  1135. * var iterator = function(num, index, done) {
  1136. * setTimeout(function() {
  1137. * order.push([num, index]);
  1138. * done(null, num % 2);
  1139. * }, num * 10);
  1140. * };
  1141. * async.pick(array, iterator, function(err, res) {
  1142. * console.log(res); // { '0': 1, '1': 3 }
  1143. * console.log(order); // [[0, 1], [2, 2], [3, 1], [4, 3]]
  1144. * });
  1145. *
  1146. * @example
  1147. *
  1148. * // object
  1149. * var order = [];
  1150. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1151. * var iterator = function(num, done) {
  1152. * setTimeout(function() {
  1153. * order.push(num);
  1154. * done(null, num % 2);
  1155. * }, num * 10);
  1156. * };
  1157. * async.pick(object, iterator, function(err, res) {
  1158. * console.log(res); // { a: 1, b: 3 }
  1159. * console.log(order); // [1, 2, 3, 4]
  1160. * });
  1161. *
  1162. * @example
  1163. *
  1164. * // object with key
  1165. * var order = [];
  1166. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1167. * var iterator = function(num, key, done) {
  1168. * setTimeout(function() {
  1169. * order.push([num, key]);
  1170. * done(null, num % 2);
  1171. * }, num * 10);
  1172. * };
  1173. * async.pick(object, iterator, function(err, res) {
  1174. * console.log(res); // { a: 1, b: 3 }
  1175. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b'], [4, 'd']]
  1176. * });
  1177. *
  1178. */
  1179. var pick = createPick(arrayEachIndexValue, baseEachKeyValue, symbolEachKeyValue, true);
  1180. /**
  1181. * @memberof async
  1182. * @namespace pickSeries
  1183. * @param {Array|Object} collection
  1184. * @param {Function} iterator
  1185. * @param {Function} callback
  1186. * @example
  1187. *
  1188. * // array
  1189. * var order = [];
  1190. * var array = [1, 3, 2, 4];
  1191. * var iterator = function(num, done) {
  1192. * setTimeout(function() {
  1193. * order.push(num);
  1194. * done(null, num % 2);
  1195. * }, num * 10);
  1196. * };
  1197. * async.pickSeries(array, iterator, function(err, res) {
  1198. * console.log(res); // { '0': 1, '1': 3 }
  1199. * console.log(order); // [1, 3, 2, 4]
  1200. * });
  1201. *
  1202. * @example
  1203. *
  1204. * // array with index
  1205. * var order = [];
  1206. * var array = [1, 3, 2, 4];
  1207. * var iterator = function(num, index, done) {
  1208. * setTimeout(function() {
  1209. * order.push([num, index]);
  1210. * done(null, num % 2);
  1211. * }, num * 10);
  1212. * };
  1213. * async.pickSeries(array, iterator, function(err, res) {
  1214. * console.log(res); // { '0': 1, '1': 3 }
  1215. * console.log(order); // [[0, 1], [3, 1], [2, 2], [4, 3]]
  1216. * });
  1217. *
  1218. * @example
  1219. *
  1220. * // object
  1221. * var order = [];
  1222. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1223. * var iterator = function(num, done) {
  1224. * setTimeout(function() {
  1225. * order.push(num);
  1226. * done(null, num % 2);
  1227. * }, num * 10);
  1228. * };
  1229. * async.pickSeries(object, iterator, function(err, res) {
  1230. * console.log(res); // { a: 1, b: 3 }
  1231. * console.log(order); // [1, 3, 2, 4]
  1232. * });
  1233. *
  1234. * @example
  1235. *
  1236. * // object with key
  1237. * var order = [];
  1238. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1239. * var iterator = function(num, key, done) {
  1240. * setTimeout(function() {
  1241. * order.push([num, key]);
  1242. * done(null, num % 2);
  1243. * }, num * 10);
  1244. * };
  1245. * async.pickSeries(object, iterator, function(err, res) {
  1246. * console.log(res); // { a: 1, b: 3 }
  1247. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c'], [4, 'd']]
  1248. * });
  1249. *
  1250. */
  1251. var pickSeries = createPickSeries(true);
  1252. /**
  1253. * @memberof async
  1254. * @namespace pickLimit
  1255. * @param {Array|Object} collection
  1256. * @param {number} limit - limit >= 1
  1257. * @param {Function} iterator
  1258. * @param {Function} callback
  1259. * @example
  1260. *
  1261. * // array
  1262. * var order = [];
  1263. * var array = [1, 5, 3, 4, 2];
  1264. * var iterator = function(num, done) {
  1265. * setTimeout(function() {
  1266. * order.push(num);
  1267. * done(null, num % 2);
  1268. * }, num * 10);
  1269. * };
  1270. * async.pickLimit(array, 2, iterator, function(err, res) {
  1271. * console.log(res); // { '0': 1, '1': 5, '2': 3 }
  1272. * console.log(order); // [1, 3, 5, 2, 4]
  1273. * });
  1274. *
  1275. * @example
  1276. *
  1277. * // array with index
  1278. * var order = [];
  1279. * var array = [1, 5, 3, 4, 2];
  1280. * var iterator = function(num, index, done) {
  1281. * setTimeout(function() {
  1282. * order.push([num, index]);
  1283. * done(null, num % 2);
  1284. * }, num * 10);
  1285. * };
  1286. * async.pickLimit(array, 2, iterator, function(err, res) {
  1287. * console.log(res); // { '0': 1, '1': 5, '2': 3 }
  1288. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  1289. * });
  1290. *
  1291. * @example
  1292. *
  1293. * // object
  1294. * var order = [];
  1295. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1296. * var iterator = function(num, done) {
  1297. * setTimeout(function() {
  1298. * order.push(num);
  1299. * done(null, num % 2);
  1300. * }, num * 10);
  1301. * };
  1302. * async.pickLimit(object, 2, iterator, function(err, res) {
  1303. * console.log(res); // { a: 1, b: 5, c: 3 }
  1304. * console.log(order); // [1, 3, 5, 2, 4]
  1305. * });
  1306. *
  1307. * @example
  1308. *
  1309. * // object with key
  1310. * var order = [];
  1311. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1312. * var iterator = function(num, key, done) {
  1313. * setTimeout(function() {
  1314. * order.push([num, key]);
  1315. * done(null, num % 2);
  1316. * }, num * 10);
  1317. * };
  1318. * async.pickLimit(object, 2, iterator, function(err, res) {
  1319. * console.log(res); // { a: 1, b: 5, c: 3 }
  1320. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  1321. * });
  1322. *
  1323. */
  1324. var pickLimit = createPickLimit(true);
  1325. /**
  1326. * @memberof async
  1327. * @namespace omit
  1328. * @param {Array|Object} collection
  1329. * @param {Function} iterator
  1330. * @param {Function} callback
  1331. * @example
  1332. *
  1333. * // array
  1334. * var order = [];
  1335. * var array = [1, 3, 2, 4];
  1336. * var iterator = function(num, done) {
  1337. * setTimeout(function() {
  1338. * order.push(num);
  1339. * done(null, num % 2);
  1340. * }, num * 10);
  1341. * };
  1342. * async.omit(array, iterator, function(err, res) {
  1343. * console.log(res); // { '2': 2, '3': 4 }
  1344. * console.log(order); // [1, 2, 3, 4]
  1345. * });
  1346. *
  1347. * @example
  1348. *
  1349. * // array with index
  1350. * var order = [];
  1351. * var array = [1, 3, 2, 4];
  1352. * var iterator = function(num, index, done) {
  1353. * setTimeout(function() {
  1354. * order.push([num, index]);
  1355. * done(null, num % 2);
  1356. * }, num * 10);
  1357. * };
  1358. * async.omit(array, iterator, function(err, res) {
  1359. * console.log(res); // { '2': 2, '3': 4 }
  1360. * console.log(order); // [[0, 1], [2, 2], [3, 1], [4, 3]]
  1361. * });
  1362. *
  1363. * @example
  1364. *
  1365. * // object
  1366. * var order = [];
  1367. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1368. * var iterator = function(num, done) {
  1369. * setTimeout(function() {
  1370. * order.push(num);
  1371. * done(null, num % 2);
  1372. * }, num * 10);
  1373. * };
  1374. * async.omit(object, iterator, function(err, res) {
  1375. * console.log(res); // { c: 2, d: 4 }
  1376. * console.log(order); // [1, 2, 3, 4]
  1377. * });
  1378. *
  1379. * @example
  1380. *
  1381. * // object with key
  1382. * var order = [];
  1383. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1384. * var iterator = function(num, key, done) {
  1385. * setTimeout(function() {
  1386. * order.push([num, key]);
  1387. * done(null, num % 2);
  1388. * }, num * 10);
  1389. * };
  1390. * async.omit(object, iterator, function(err, res) {
  1391. * console.log(res); // { c: 2, d: 4 }
  1392. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b'], [4, 'd']]
  1393. * });
  1394. *
  1395. */
  1396. var omit = createPick(arrayEachIndexValue, baseEachKeyValue, symbolEachKeyValue, false);
  1397. /**
  1398. * @memberof async
  1399. * @namespace omitSeries
  1400. * @param {Array|Object} collection
  1401. * @param {Function} iterator
  1402. * @param {Function} callback
  1403. * @example
  1404. *
  1405. * // array
  1406. * var order = [];
  1407. * var array = [1, 3, 2, 4];
  1408. * var iterator = function(num, done) {
  1409. * setTimeout(function() {
  1410. * order.push(num);
  1411. * done(null, num % 2);
  1412. * }, num * 10);
  1413. * };
  1414. * async.omitSeries(array, iterator, function(err, res) {
  1415. * console.log(res); // { '2': 2, '3': 4 }
  1416. * console.log(order); // [1, 3, 2, 4]
  1417. * });
  1418. *
  1419. * @example
  1420. *
  1421. * // array with index
  1422. * var order = [];
  1423. * var array = [1, 3, 2, 4];
  1424. * var iterator = function(num, index, done) {
  1425. * setTimeout(function() {
  1426. * order.push([num, index]);
  1427. * done(null, num % 2);
  1428. * }, num * 10);
  1429. * };
  1430. * async.omitSeries(array, iterator, function(err, res) {
  1431. * console.log(res); // { '2': 2, '3': 4 }
  1432. * console.log(order); // [[0, 1], [3, 1], [2, 2], [4, 3]]
  1433. * });
  1434. *
  1435. * @example
  1436. *
  1437. * // object
  1438. * var order = [];
  1439. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1440. * var iterator = function(num, done) {
  1441. * setTimeout(function() {
  1442. * order.push(num);
  1443. * done(null, num % 2);
  1444. * }, num * 10);
  1445. * };
  1446. * async.omitSeries(object, iterator, function(err, res) {
  1447. * console.log(res); // { c: 2, d: 4 }
  1448. * console.log(order); // [1, 3, 2, 4]
  1449. * });
  1450. *
  1451. * @example
  1452. *
  1453. * // object with key
  1454. * var order = [];
  1455. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1456. * var iterator = function(num, key, done) {
  1457. * setTimeout(function() {
  1458. * order.push([num, key]);
  1459. * done(null, num % 2);
  1460. * }, num * 10);
  1461. * };
  1462. * async.omitSeries(object, iterator, function(err, res) {
  1463. * console.log(res); // { c: 2, d: 4 }
  1464. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c'], [4, 'd']]
  1465. * });
  1466. *
  1467. */
  1468. var omitSeries = createPickSeries(false);
  1469. /**
  1470. * @memberof async
  1471. * @namespace omitLimit
  1472. * @param {Array|Object} collection
  1473. * @param {number} limit - limit >= 1
  1474. * @param {Function} iterator
  1475. * @param {Function} callback
  1476. * @example
  1477. *
  1478. * // array
  1479. * var order = [];
  1480. * var array = [1, 5, 3, 4, 2];
  1481. * var iterator = function(num, done) {
  1482. * setTimeout(function() {
  1483. * order.push(num);
  1484. * done(null, num % 2);
  1485. * }, num * 10);
  1486. * };
  1487. * async.omitLimit(array, 2, iterator, function(err, res) {
  1488. * console.log(res); // { '3': 4, '4': 2 }
  1489. * console.log(order); // [1, 3, 5, 2, 4]
  1490. * });
  1491. *
  1492. * @example
  1493. *
  1494. * // array with index
  1495. * var order = [];
  1496. * var array = [1, 5, 3, 4, 2];
  1497. * var iterator = function(num, index, done) {
  1498. * setTimeout(function() {
  1499. * order.push([num, index]);
  1500. * done(null, num % 2);
  1501. * }, num * 10);
  1502. * };
  1503. * async.omitLimit(array, 2, iterator, function(err, res) {
  1504. * console.log(res); // { '3': 4, '4': 2 }
  1505. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  1506. * });
  1507. *
  1508. * @example
  1509. *
  1510. * // object
  1511. * var order = [];
  1512. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1513. * var iterator = function(num, done) {
  1514. * setTimeout(function() {
  1515. * order.push(num);
  1516. * done(null, num % 2);
  1517. * }, num * 10);
  1518. * };
  1519. * async.omitLimit(object, 2, iterator, function(err, res) {
  1520. * console.log(res); // { d: 4, e: 2 }
  1521. * console.log(order); // [1, 3, 5, 2, 4]
  1522. * });
  1523. *
  1524. * @example
  1525. *
  1526. * // object with key
  1527. * var order = [];
  1528. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  1529. * var iterator = function(num, key, done) {
  1530. * setTimeout(function() {
  1531. * order.push([num, key]);
  1532. * done(null, num % 2);
  1533. * }, num * 10);
  1534. * };
  1535. * async.omitLimit(object, 2, iterator, function(err, res) {
  1536. * console.log(res); // { d: 4, e: 2 }
  1537. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  1538. * });
  1539. *
  1540. */
  1541. var omitLimit = createPickLimit(false);
  1542. /**
  1543. * @memberof async
  1544. * @namespace transform
  1545. * @param {Array|Object} collection
  1546. * @param {Array|Object|Function} [accumulator]
  1547. * @param {Function} [iterator]
  1548. * @param {Function} [callback]
  1549. * @example
  1550. *
  1551. * // array
  1552. * var order = [];
  1553. * var collection = [1, 3, 2, 4];
  1554. * var iterator = function(result, num, done) {
  1555. * setTimeout(function() {
  1556. * order.push(num);
  1557. * result.push(num)
  1558. * done();
  1559. * }, num * 10);
  1560. * };
  1561. * async.transform(collection, iterator, function(err, res) {
  1562. * console.log(res); // [1, 2, 3, 4]
  1563. * console.log(order); // [1, 2, 3, 4]
  1564. * });
  1565. *
  1566. * @example
  1567. *
  1568. * // array with index and accumulator
  1569. * var order = [];
  1570. * var collection = [1, 3, 2, 4];
  1571. * var iterator = function(result, num, index, done) {
  1572. * setTimeout(function() {
  1573. * order.push([num, index]);
  1574. * result[index] = num;
  1575. * done();
  1576. * }, num * 10);
  1577. * };
  1578. * async.transform(collection, {}, iterator, function(err, res) {
  1579. * console.log(res); // { '0': 1, '1': 3, '2': 2, '3': 4 }
  1580. * console.log(order); // [[1, 0], [2, 2], [3, 1], [4, 3]]
  1581. * });
  1582. *
  1583. * @example
  1584. *
  1585. * // object with accumulator
  1586. * var order = [];
  1587. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1588. * var iterator = function(result, num, done) {
  1589. * setTimeout(function() {
  1590. * order.push(num);
  1591. * result.push(num);
  1592. * done();
  1593. * }, num * 10);
  1594. * };
  1595. * async.transform(collection, [], iterator, function(err, res) {
  1596. * console.log(res); // [1, 2, 3, 4]
  1597. * console.log(order); // [1, 2, 3, 4]
  1598. * });
  1599. *
  1600. * @example
  1601. *
  1602. * // object with key
  1603. * var order = [];
  1604. * var object = { a: 1, b: 3, c: 2, d: 4 };
  1605. * var iterator = function(result, num, key, done) {
  1606. * setTimeout(function() {
  1607. * order.push([num, key]);
  1608. * result[key] = num;
  1609. * done();
  1610. * }, num * 10);
  1611. * };
  1612. * async.transform(collection, iterator, function(err, res) {
  1613. * console.log(res); // { a: 1, b: 3, c: 2, d: 4 }
  1614. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b'], [4, 'd']]
  1615. * });
  1616. *
  1617. */
  1618. var transform = createTransform(arrayEachResult, baseEachResult, symbolEachResult);
  1619. /**
  1620. * @memberof async
  1621. * @namespace sortBy
  1622. * @param {Array|Object} collection
  1623. * @param {Function} iterator
  1624. * @param {Function} callback
  1625. * @example
  1626. *
  1627. * // array
  1628. * var order = [];
  1629. * var array = [1, 3, 2];
  1630. * var iterator = function(num, done) {
  1631. * setTimeout(function() {
  1632. * order.push(num);
  1633. * done(null, num);
  1634. * }, num * 10);
  1635. * };
  1636. * async.sortBy(array, iterator, function(err, res) {
  1637. * console.log(res); // [1, 2, 3];
  1638. * console.log(order); // [1, 2, 3]
  1639. * });
  1640. *
  1641. * @example
  1642. *
  1643. * // array with index
  1644. * var order = [];
  1645. * var array = [1, 3, 2];
  1646. * var iterator = function(num, index, done) {
  1647. * setTimeout(function() {
  1648. * order.push([num, index]);
  1649. * done(null, num);
  1650. * }, num * 10);
  1651. * };
  1652. * async.sortBy(array, iterator, function(err, res) {
  1653. * console.log(res); // [1, 2, 3]
  1654. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  1655. * });
  1656. *
  1657. * @example
  1658. *
  1659. * // object
  1660. * var order = [];
  1661. * var object = { a: 1, b: 3, c: 2 };
  1662. * var iterator = function(num, done) {
  1663. * setTimeout(function() {
  1664. * order.push(num);
  1665. * done(null, num);
  1666. * }, num * 10);
  1667. * };
  1668. * async.sortBy(object, iterator, function(err, res) {
  1669. * console.log(res); // [1, 2, 3]
  1670. * console.log(order); // [1, 2, 3]
  1671. * });
  1672. *
  1673. * @example
  1674. *
  1675. * // object with key
  1676. * var order = [];
  1677. * var object = { a: 1, b: 3, c: 2 };
  1678. * var iterator = function(num, key, done) {
  1679. * setTimeout(function() {
  1680. * order.push([num, key]);
  1681. * done(null, num);
  1682. * }, num * 10);
  1683. * };
  1684. * async.sortBy(object, iterator, function(err, res) {
  1685. * console.log(res); // [1, 2, 3]
  1686. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  1687. * });
  1688. *
  1689. */
  1690. var sortBy = createSortBy(arrayEachIndexValue, baseEachIndexValue, symbolEachIndexValue);
  1691. /**
  1692. * @memberof async
  1693. * @namespace concat
  1694. * @param {Array|Object} collection
  1695. * @param {Function} iterator
  1696. * @param {Function} callback
  1697. * @example
  1698. *
  1699. * // array
  1700. * var order = [];
  1701. * var array = [1, 3, 2];
  1702. * var iterator = function(num, done) {
  1703. * setTimeout(function() {
  1704. * order.push(num);
  1705. * done(null, [num]);
  1706. * }, num * 10);
  1707. * };
  1708. * async.concat(array, iterator, function(err, res) {
  1709. * console.log(res); // [1, 2, 3];
  1710. * console.log(order); // [1, 2, 3]
  1711. * });
  1712. *
  1713. * @example
  1714. *
  1715. * // array with index
  1716. * var order = [];
  1717. * var array = [1, 3, 2];
  1718. * var iterator = function(num, index, done) {
  1719. * setTimeout(function() {
  1720. * order.push([num, index]);
  1721. * done(null, [num]);
  1722. * }, num * 10);
  1723. * };
  1724. * async.concat(array, iterator, function(err, res) {
  1725. * console.log(res); // [1, 2, 3]
  1726. * console.log(order); // [[1, 0], [2, 2], [3, 1]]
  1727. * });
  1728. *
  1729. * @example
  1730. *
  1731. * // object
  1732. * var order = [];
  1733. * var object = { a: 1, b: 3, c: 2 };
  1734. * var iterator = function(num, done) {
  1735. * setTimeout(function() {
  1736. * order.push(num);
  1737. * done(null, [num]);
  1738. * }, num * 10);
  1739. * };
  1740. * async.concat(object, iterator, function(err, res) {
  1741. * console.log(res); // [1, 2, 3]
  1742. * console.log(order); // [1, 2, 3]
  1743. * });
  1744. *
  1745. * @example
  1746. *
  1747. * // object with key
  1748. * var order = [];
  1749. * var object = { a: 1, b: 3, c: 2 };
  1750. * var iterator = function(num, key, done) {
  1751. * setTimeout(function() {
  1752. * order.push([num, key]);
  1753. * done(null, [num]);
  1754. * }, num * 10);
  1755. * };
  1756. * async.concat(object, iterator, function(err, res) {
  1757. * console.log(res); // [1, 2, 3]
  1758. * console.log(order); // [[1, 'a'], [2, 'c'], [3, 'b']]
  1759. * });
  1760. *
  1761. */
  1762. var concat = createConcat(arrayEachIndex, baseEachIndex, symbolEachIndex);
  1763. /**
  1764. * @memberof async
  1765. * @namespace groupBy
  1766. * @param {Array|Object} collection
  1767. * @param {Function} iterator
  1768. * @param {Function} callback
  1769. * @example
  1770. *
  1771. * // array
  1772. * var order = [];
  1773. * var array = [4.2, 6.4, 6.1];
  1774. * var iterator = function(num, done) {
  1775. * setTimeout(function() {
  1776. * order.push(num);
  1777. * done(null, Math.floor(num));
  1778. * }, num * 10);
  1779. * };
  1780. * async.groupBy(array, iterator, function(err, res) {
  1781. * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  1782. * console.log(order); // [4.2, 6.1, 6.4]
  1783. * });
  1784. *
  1785. * @example
  1786. *
  1787. * // array with index
  1788. * var order = [];
  1789. * var array = [4.2, 6.4, 6.1];
  1790. * var iterator = function(num, index, done) {
  1791. * setTimeout(function() {
  1792. * order.push([num, index]);
  1793. * done(null, Math.floor(num));
  1794. * }, num * 10);
  1795. * };
  1796. * async.groupBy(array, iterator, function(err, res) {
  1797. * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  1798. * console.log(order); // [[4.2, 0], [6.1, 2], [6.4, 1]]
  1799. * });
  1800. *
  1801. * @example
  1802. *
  1803. * // object
  1804. * var order = [];
  1805. * var object = { a: 4.2, b: 6.4, c: 6.1 };
  1806. * var iterator = function(num, done) {
  1807. * setTimeout(function() {
  1808. * order.push(num);
  1809. * done(null, Math.floor(num));
  1810. * }, num * 10);
  1811. * };
  1812. * async.groupBy(object, iterator, function(err, res) {
  1813. * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  1814. * console.log(order); // [4.2, 6.1, 6.4]
  1815. * });
  1816. *
  1817. * @example
  1818. *
  1819. * // object with key
  1820. * var order = [];
  1821. * var object = { a: 4.2, b: 6.4, c: 6.1 };
  1822. * var iterator = function(num, key, done) {
  1823. * setTimeout(function() {
  1824. * order.push([num, key]);
  1825. * done(null, Math.floor(num));
  1826. * }, num * 10);
  1827. * };
  1828. * async.groupBy(object, iterator, function(err, res) {
  1829. * console.log(res); // { '4': [4.2], '6': [6.1, 6.4] }
  1830. * console.log(order); // [[4.2, 'a'], [6.1, 'c'], [6.4, 'b']]
  1831. * });
  1832. *
  1833. */
  1834. var groupBy = createGroupBy(arrayEachValue, baseEachValue, symbolEachValue);
  1835. /**
  1836. * @memberof async
  1837. * @namespace parallel
  1838. * @param {Array|Object} tasks - functions
  1839. * @param {Function} callback
  1840. * @example
  1841. *
  1842. * var order = [];
  1843. * var tasks = [
  1844. * function(done) {
  1845. * setTimeout(function() {
  1846. * order.push(1);
  1847. * done(null, 1);
  1848. * }, 10);
  1849. * },
  1850. * function(done) {
  1851. * setTimeout(function() {
  1852. * order.push(2);
  1853. * done(null, 2);
  1854. * }, 30);
  1855. * },
  1856. * function(done) {
  1857. * setTimeout(function() {
  1858. * order.push(3);
  1859. * done(null, 3);
  1860. * }, 40);
  1861. * },
  1862. * function(done) {
  1863. * setTimeout(function() {
  1864. * order.push(4);
  1865. * done(null, 4);
  1866. * }, 20);
  1867. * }
  1868. * ];
  1869. * async.parallel(tasks, function(err, res) {
  1870. * console.log(res); // [1, 2, 3, 4];
  1871. * console.log(order); // [1, 4, 2, 3]
  1872. * });
  1873. *
  1874. * @example
  1875. *
  1876. * var order = [];
  1877. * var tasks = {
  1878. * 'a': function(done) {
  1879. * setTimeout(function() {
  1880. * order.push(1);
  1881. * done(null, 1);
  1882. * }, 10);
  1883. * },
  1884. * 'b': function(done) {
  1885. * setTimeout(function() {
  1886. * order.push(2);
  1887. * done(null, 2);
  1888. * }, 30);
  1889. * },
  1890. * 'c': function(done) {
  1891. * setTimeout(function() {
  1892. * order.push(3);
  1893. * done(null, 3);
  1894. * }, 40);
  1895. * },
  1896. * 'd': function(done) {
  1897. * setTimeout(function() {
  1898. * order.push(4);
  1899. * done(null, 4);
  1900. * }, 20);
  1901. * }
  1902. * };
  1903. * async.parallel(tasks, function(err, res) {
  1904. * console.log(res); // { a: 1, b: 2, c: 3, d:4 }
  1905. * console.log(order); // [1, 4, 2, 3]
  1906. * });
  1907. *
  1908. */
  1909. var parallel = createParallel(arrayEachFunc, baseEachFunc);
  1910. /**
  1911. * @memberof async
  1912. * @namespace applyEach
  1913. */
  1914. var applyEach = createApplyEach(map);
  1915. /**
  1916. * @memberof async
  1917. * @namespace applyEachSeries
  1918. */
  1919. var applyEachSeries = createApplyEach(mapSeries);
  1920. /**
  1921. * @memberof async
  1922. * @namespace log
  1923. */
  1924. var log = createLogger('log');
  1925. /**
  1926. * @memberof async
  1927. * @namespace dir
  1928. */
  1929. var dir = createLogger('dir');
  1930. /**
  1931. * @version 2.5.1
  1932. * @namespace async
  1933. */
  1934. var index = {
  1935. VERSION: '2.5.1',
  1936. // Collections
  1937. each: each,
  1938. eachSeries: eachSeries,
  1939. eachLimit: eachLimit,
  1940. forEach: each,
  1941. forEachSeries: eachSeries,
  1942. forEachLimit: eachLimit,
  1943. eachOf: each,
  1944. eachOfSeries: eachSeries,
  1945. eachOfLimit: eachLimit,
  1946. forEachOf: each,
  1947. forEachOfSeries: eachSeries,
  1948. forEachOfLimit: eachLimit,
  1949. map: map,
  1950. mapSeries: mapSeries,
  1951. mapLimit: mapLimit,
  1952. mapValues: mapValues,
  1953. mapValuesSeries: mapValuesSeries,
  1954. mapValuesLimit: mapValuesLimit,
  1955. filter: filter,
  1956. filterSeries: filterSeries,
  1957. filterLimit: filterLimit,
  1958. select: filter,
  1959. selectSeries: filterSeries,
  1960. selectLimit: filterLimit,
  1961. reject: reject,
  1962. rejectSeries: rejectSeries,
  1963. rejectLimit: rejectLimit,
  1964. detect: detect,
  1965. detectSeries: detectSeries,
  1966. detectLimit: detectLimit,
  1967. find: detect,
  1968. findSeries: detectSeries,
  1969. findLimit: detectLimit,
  1970. pick: pick,
  1971. pickSeries: pickSeries,
  1972. pickLimit: pickLimit,
  1973. omit: omit,
  1974. omitSeries: omitSeries,
  1975. omitLimit: omitLimit,
  1976. reduce: reduce,
  1977. inject: reduce,
  1978. foldl: reduce,
  1979. reduceRight: reduceRight,
  1980. foldr: reduceRight,
  1981. transform: transform,
  1982. transformSeries: transformSeries,
  1983. transformLimit: transformLimit,
  1984. sortBy: sortBy,
  1985. sortBySeries: sortBySeries,
  1986. sortByLimit: sortByLimit,
  1987. some: some,
  1988. someSeries: someSeries,
  1989. someLimit: someLimit,
  1990. any: some,
  1991. anySeries: someSeries,
  1992. anyLimit: someLimit,
  1993. every: every,
  1994. everySeries: everySeries,
  1995. everyLimit: everyLimit,
  1996. all: every,
  1997. allSeries: everySeries,
  1998. allLimit: everyLimit,
  1999. concat: concat,
  2000. concatSeries: concatSeries,
  2001. concatLimit: concatLimit,
  2002. groupBy: groupBy,
  2003. groupBySeries: groupBySeries,
  2004. groupByLimit: groupByLimit,
  2005. // Control Flow
  2006. parallel: parallel,
  2007. series: series,
  2008. parallelLimit: parallelLimit,
  2009. tryEach: tryEach,
  2010. waterfall: waterfall,
  2011. angelFall: angelFall,
  2012. angelfall: angelFall,
  2013. whilst: whilst,
  2014. doWhilst: doWhilst,
  2015. until: until,
  2016. doUntil: doUntil,
  2017. during: during,
  2018. doDuring: doDuring,
  2019. forever: forever,
  2020. compose: compose,
  2021. seq: seq,
  2022. applyEach: applyEach,
  2023. applyEachSeries: applyEachSeries,
  2024. queue: queue,
  2025. priorityQueue: priorityQueue,
  2026. cargo: cargo,
  2027. auto: auto,
  2028. autoInject: autoInject,
  2029. retry: retry,
  2030. retryable: retryable,
  2031. iterator: iterator,
  2032. times: times,
  2033. timesSeries: timesSeries,
  2034. timesLimit: timesLimit,
  2035. race: race,
  2036. // Utils
  2037. apply: apply,
  2038. nextTick: asyncNextTick,
  2039. setImmediate: asyncSetImmediate,
  2040. memoize: memoize,
  2041. unmemoize: unmemoize,
  2042. ensureAsync: ensureAsync,
  2043. constant: constant,
  2044. asyncify: asyncify,
  2045. wrapSync: asyncify,
  2046. log: log,
  2047. dir: dir,
  2048. reflect: reflect,
  2049. reflectAll: reflectAll,
  2050. timeout: timeout,
  2051. createLogger: createLogger,
  2052. // Mode
  2053. safe: safe,
  2054. fast: fast
  2055. };
  2056. exports['default'] = index;
  2057. baseEachSync(index, function(func, key) {
  2058. exports[key] = func;
  2059. }, nativeKeys(index));
  2060. /**
  2061. * @private
  2062. */
  2063. function createImmediate(safeMode) {
  2064. var delay = function delay(fn) {
  2065. var args = slice(arguments, 1);
  2066. setTimeout(function() {
  2067. fn.apply(null, args);
  2068. });
  2069. };
  2070. asyncSetImmediate = typeof setImmediate === func ? setImmediate : delay;
  2071. if (typeof process === obj && typeof process.nextTick === func) {
  2072. nextTick = /^v0.10/.test(process.version) ? asyncSetImmediate : process.nextTick;
  2073. asyncNextTick = /^v0/.test(process.version) ? asyncSetImmediate : process.nextTick;
  2074. } else {
  2075. asyncNextTick = nextTick = asyncSetImmediate;
  2076. }
  2077. if (safeMode === false) {
  2078. nextTick = function(cb) {
  2079. cb();
  2080. };
  2081. }
  2082. }
  2083. /* sync functions based on lodash */
  2084. /**
  2085. * Converts `arguments` to an array.
  2086. *
  2087. * @private
  2088. * @param {Array} array = The array to slice.
  2089. */
  2090. function createArray(array) {
  2091. var index = -1;
  2092. var size = array.length;
  2093. var result = Array(size);
  2094. while (++index < size) {
  2095. result[index] = array[index];
  2096. }
  2097. return result;
  2098. }
  2099. /**
  2100. * Create an array from `start`
  2101. *
  2102. * @private
  2103. * @param {Array} array - The array to slice.
  2104. * @param {number} start - The start position.
  2105. */
  2106. function slice(array, start) {
  2107. var end = array.length;
  2108. var index = -1;
  2109. var size = end - start;
  2110. if (size <= 0) {
  2111. return [];
  2112. }
  2113. var result = Array(size);
  2114. while (++index < size) {
  2115. result[index] = array[index + start];
  2116. }
  2117. return result;
  2118. }
  2119. /**
  2120. * @private
  2121. * @param {Object} object
  2122. */
  2123. function objectClone(object) {
  2124. var keys = nativeKeys(object);
  2125. var size = keys.length;
  2126. var index = -1;
  2127. var result = {};
  2128. while (++index < size) {
  2129. var key = keys[index];
  2130. result[key] = object[key];
  2131. }
  2132. return result;
  2133. }
  2134. /**
  2135. * Create an array with all falsey values removed.
  2136. *
  2137. * @private
  2138. * @param {Array} array - The array to compact.
  2139. */
  2140. function compact(array) {
  2141. var index = -1;
  2142. var size = array.length;
  2143. var result = [];
  2144. while (++index < size) {
  2145. var value = array[index];
  2146. if (value) {
  2147. result[result.length] = value;
  2148. }
  2149. }
  2150. return result;
  2151. }
  2152. /**
  2153. * Create an array of reverse sequence.
  2154. *
  2155. * @private
  2156. * @param {Array} array - The array to reverse.
  2157. */
  2158. function reverse(array) {
  2159. var index = -1;
  2160. var size = array.length;
  2161. var result = Array(size);
  2162. var resIndex = size;
  2163. while (++index < size) {
  2164. result[--resIndex] = array[index];
  2165. }
  2166. return result;
  2167. }
  2168. /**
  2169. * Checks if key exists in object property.
  2170. *
  2171. * @private
  2172. * @param {Object} object - The object to inspect.
  2173. * @param {string} key - The key to check.
  2174. */
  2175. function has(object, key) {
  2176. return object.hasOwnProperty(key);
  2177. }
  2178. /**
  2179. * Check if target exists in array.
  2180. * @private
  2181. * @param {Array} array
  2182. * @param {*} target
  2183. */
  2184. function notInclude(array, target) {
  2185. var index = -1;
  2186. var size = array.length;
  2187. while(++index < size) {
  2188. if (array[index] === target) {
  2189. return false;
  2190. }
  2191. }
  2192. return true;
  2193. }
  2194. /**
  2195. * @private
  2196. * @param {Array} array - The array to iterate over.
  2197. * @param {Function} iterator - The function invoked per iteration.
  2198. */
  2199. function arrayEachSync(array, iterator) {
  2200. var index = -1;
  2201. var size = array.length;
  2202. while (++index < size) {
  2203. iterator(array[index], index);
  2204. }
  2205. return array;
  2206. }
  2207. /**
  2208. * @private
  2209. * @param {Object} object - The object to iterate over.
  2210. * @param {Function} iterator - The function invoked per iteration.
  2211. * @param {Array} keys
  2212. */
  2213. function baseEachSync(object, iterator, keys) {
  2214. var index = -1;
  2215. var size = keys.length;
  2216. while (++index < size) {
  2217. var key = keys[index];
  2218. iterator(object[key], key);
  2219. }
  2220. return object;
  2221. }
  2222. /**
  2223. * @private
  2224. * @param {number} n
  2225. * @param {Function} iterator
  2226. */
  2227. function timesSync(n, iterator) {
  2228. var index = -1;
  2229. while (++index < n) {
  2230. iterator(index);
  2231. }
  2232. }
  2233. /**
  2234. * @private
  2235. * @param {Array} array
  2236. * @param {number[]} criteria
  2237. */
  2238. function sortByCriteria(array, criteria) {
  2239. var l = array.length;
  2240. var indices = Array(l);
  2241. var i;
  2242. for (i = 0; i < l; i++) {
  2243. indices[i] = i;
  2244. }
  2245. quickSort(criteria, 0, l - 1, indices);
  2246. var result = Array(l);
  2247. for (var n = 0; n < l; n++) {
  2248. i = indices[n];
  2249. result[n] = i === undefined ? array[n] : array[i];
  2250. }
  2251. return result;
  2252. }
  2253. function partition(array, i, j, mid, indices) {
  2254. var l = i;
  2255. var r = j;
  2256. while (l <= r) {
  2257. i = l;
  2258. while (l < r && array[l] < mid) {
  2259. l++;
  2260. }
  2261. while (r >= i && array[r] >= mid) {
  2262. r--;
  2263. }
  2264. if (l > r) {
  2265. break;
  2266. }
  2267. swap(array, indices, l++, r--);
  2268. }
  2269. return l;
  2270. }
  2271. function swap(array, indices, l, r) {
  2272. var n = array[l];
  2273. array[l] = array[r];
  2274. array[r] = n;
  2275. var i = indices[l];
  2276. indices[l] = indices[r];
  2277. indices[r] = i;
  2278. }
  2279. function quickSort(array, i, j, indices) {
  2280. if (i === j) {
  2281. return;
  2282. }
  2283. var k = i;
  2284. while (++k <= j && array[i] === array[k]) {
  2285. var l = k - 1;
  2286. if (indices[l] > indices[k]) {
  2287. var index = indices[l];
  2288. indices[l] = indices[k];
  2289. indices[k] = index;
  2290. }
  2291. }
  2292. if (k > j) {
  2293. return;
  2294. }
  2295. var p = array[i] > array[k] ? i : k;
  2296. k = partition(array, i, j, array[p], indices);
  2297. quickSort(array, i, k - 1, indices);
  2298. quickSort(array, k, j, indices);
  2299. }
  2300. /**
  2301. * @Private
  2302. */
  2303. function makeConcatResult(array) {
  2304. var result = [];
  2305. arrayEachSync(array, function(value) {
  2306. if (value === noop) {
  2307. return;
  2308. }
  2309. if (isArray(value)) {
  2310. nativePush.apply(result, value);
  2311. } else {
  2312. result.push(value);
  2313. }
  2314. });
  2315. return result;
  2316. }
  2317. /* async functions */
  2318. /**
  2319. * @private
  2320. */
  2321. function arrayEach(array, iterator, callback) {
  2322. var index = -1;
  2323. var size = array.length;
  2324. if (iterator.length === 3) {
  2325. while (++index < size) {
  2326. iterator(array[index], index, onlyOnce(callback));
  2327. }
  2328. } else {
  2329. while (++index < size) {
  2330. iterator(array[index], onlyOnce(callback));
  2331. }
  2332. }
  2333. }
  2334. /**
  2335. * @private
  2336. */
  2337. function baseEach(object, iterator, callback, keys) {
  2338. var key;
  2339. var index = -1;
  2340. var size = keys.length;
  2341. if (iterator.length === 3) {
  2342. while (++index < size) {
  2343. key = keys[index];
  2344. iterator(object[key], key, onlyOnce(callback));
  2345. }
  2346. } else {
  2347. while (++index < size) {
  2348. iterator(object[keys[index]], onlyOnce(callback));
  2349. }
  2350. }
  2351. }
  2352. /**
  2353. * @private
  2354. */
  2355. function symbolEach(collection, iterator, callback) {
  2356. var iter = collection[iteratorSymbol]();
  2357. var index = -1;
  2358. var item;
  2359. if (iterator.length === 3) {
  2360. while ((item = iter.next()).done === false) {
  2361. iterator(item.value, ++index, onlyOnce(callback));
  2362. }
  2363. } else {
  2364. while ((item = iter.next()).done === false) {
  2365. iterator(item.value, onlyOnce(callback));
  2366. }
  2367. }
  2368. }
  2369. /**
  2370. * @private
  2371. */
  2372. function arrayEachResult(array, result, iterator, callback) {
  2373. var index = -1;
  2374. var size = array.length;
  2375. if (iterator.length === 4) {
  2376. while (++index < size) {
  2377. iterator(result, array[index], index, onlyOnce(callback));
  2378. }
  2379. } else {
  2380. while (++index < size) {
  2381. iterator(result, array[index], onlyOnce(callback));
  2382. }
  2383. }
  2384. }
  2385. /**
  2386. * @private
  2387. */
  2388. function baseEachResult(object, result, iterator, callback, keys) {
  2389. var key;
  2390. var index = -1;
  2391. var size = keys.length;
  2392. if (iterator.length === 4) {
  2393. while (++index < size) {
  2394. key = keys[index];
  2395. iterator(result, object[key], key, onlyOnce(callback));
  2396. }
  2397. } else {
  2398. while (++index < size) {
  2399. iterator(result, object[keys[index]], onlyOnce(callback));
  2400. }
  2401. }
  2402. }
  2403. /**
  2404. * @private
  2405. */
  2406. function symbolEachResult(collection, result, iterator, callback) {
  2407. var item;
  2408. var index = -1;
  2409. var iter = collection[iteratorSymbol]();
  2410. if (iterator.length === 4) {
  2411. while ((item = iter.next()).done === false) {
  2412. iterator(result, item.value, ++index, onlyOnce(callback));
  2413. }
  2414. } else {
  2415. while ((item = iter.next()).done === false) {
  2416. iterator(result, item.value, onlyOnce(callback));
  2417. }
  2418. }
  2419. }
  2420. /**
  2421. * @private
  2422. */
  2423. function arrayEachFunc(array, createCallback) {
  2424. var index = -1;
  2425. var size = array.length;
  2426. while (++index < size) {
  2427. array[index](createCallback(index));
  2428. }
  2429. }
  2430. /**
  2431. * @private
  2432. */
  2433. function baseEachFunc(object, createCallback, keys) {
  2434. var key;
  2435. var index = -1;
  2436. var size = keys.length;
  2437. while (++index < size) {
  2438. key = keys[index];
  2439. object[key](createCallback(key));
  2440. }
  2441. }
  2442. /**
  2443. * @private
  2444. */
  2445. function arrayEachIndex(array, iterator, createCallback) {
  2446. var index = -1;
  2447. var size = array.length;
  2448. if (iterator.length === 3) {
  2449. while (++index < size) {
  2450. iterator(array[index], index, createCallback(index));
  2451. }
  2452. } else {
  2453. while (++index < size) {
  2454. iterator(array[index], createCallback(index));
  2455. }
  2456. }
  2457. }
  2458. /**
  2459. * @private
  2460. */
  2461. function baseEachIndex(object, iterator, createCallback, keys) {
  2462. var key;
  2463. var index = -1;
  2464. var size = keys.length;
  2465. if (iterator.length === 3) {
  2466. while (++index < size) {
  2467. key = keys[index];
  2468. iterator(object[key], key, createCallback(index));
  2469. }
  2470. } else {
  2471. while (++index < size) {
  2472. iterator(object[keys[index]], createCallback(index));
  2473. }
  2474. }
  2475. }
  2476. /**
  2477. * @private
  2478. */
  2479. function symbolEachIndex(collection, iterator, createCallback) {
  2480. var values;
  2481. var index = -1;
  2482. var size = collection.size;
  2483. var iter = collection[iteratorSymbol]();
  2484. if (iterator.length === 3) {
  2485. while (++index < size) {
  2486. values = iter.next().value;
  2487. iterator(values, index, createCallback(index));
  2488. }
  2489. } else {
  2490. while (++index < size) {
  2491. iterator(iter.next().value, createCallback(index));
  2492. }
  2493. }
  2494. }
  2495. /**
  2496. * @private
  2497. */
  2498. function baseEachKey(object, iterator, createCallback, keys) {
  2499. var key;
  2500. var index = -1;
  2501. var size = keys.length;
  2502. if (iterator.length === 3) {
  2503. while (++index < size) {
  2504. key = keys[index];
  2505. iterator(object[key], key, createCallback(key));
  2506. }
  2507. } else {
  2508. while (++index < size) {
  2509. key = keys[index];
  2510. iterator(object[key], createCallback(key));
  2511. }
  2512. }
  2513. }
  2514. /**
  2515. * @private
  2516. */
  2517. function symbolEachKey(collection, iterator, createCallback) {
  2518. var item;
  2519. var index = -1;
  2520. var iter = collection[iteratorSymbol]();
  2521. if (iterator.length === 3) {
  2522. while ((item = iter.next()).done === false) {
  2523. iterator(item.value, ++index, createCallback(index));
  2524. }
  2525. } else {
  2526. while ((item = iter.next()).done === false) {
  2527. iterator(item.value, createCallback(++index));
  2528. }
  2529. }
  2530. }
  2531. /**
  2532. * @private
  2533. */
  2534. function arrayEachValue(array, iterator, createCallback) {
  2535. var value;
  2536. var index = -1;
  2537. var size = array.length;
  2538. if (iterator.length === 3) {
  2539. while (++index < size) {
  2540. value = array[index];
  2541. iterator(value, index, createCallback(value));
  2542. }
  2543. } else {
  2544. while (++index < size) {
  2545. value = array[index];
  2546. iterator(value, createCallback(value));
  2547. }
  2548. }
  2549. }
  2550. /**
  2551. * @private
  2552. */
  2553. function baseEachValue(object, iterator, createCallback, keys) {
  2554. var key, value;
  2555. var index = -1;
  2556. var size = keys.length;
  2557. if (iterator.length === 3) {
  2558. while (++index < size) {
  2559. key = keys[index];
  2560. value = object[key];
  2561. iterator(value, key, createCallback(value));
  2562. }
  2563. } else {
  2564. while (++index < size) {
  2565. value = object[keys[index]];
  2566. iterator(value, createCallback(value));
  2567. }
  2568. }
  2569. }
  2570. /**
  2571. * @private
  2572. */
  2573. function symbolEachValue(collection, iterator, createCallback) {
  2574. var value, item;
  2575. var index = -1;
  2576. var iter = collection[iteratorSymbol]();
  2577. if (iterator.length === 3) {
  2578. while ((item = iter.next()).done === false) {
  2579. value = item.value;
  2580. iterator(value, ++index, createCallback(value));
  2581. }
  2582. } else {
  2583. while ((item = iter.next()).done === false) {
  2584. value = item.value;
  2585. iterator(value, createCallback(value));
  2586. }
  2587. }
  2588. }
  2589. /**
  2590. * @private
  2591. */
  2592. function arrayEachIndexValue(array, iterator, createCallback) {
  2593. var value;
  2594. var index = -1;
  2595. var size = array.length;
  2596. if (iterator.length === 3) {
  2597. while (++index < size) {
  2598. value = array[index];
  2599. iterator(value, index, createCallback(index, value));
  2600. }
  2601. } else {
  2602. while (++index < size) {
  2603. value = array[index];
  2604. iterator(value, createCallback(index, value));
  2605. }
  2606. }
  2607. }
  2608. /**
  2609. * @private
  2610. */
  2611. function baseEachIndexValue(object, iterator, createCallback, keys) {
  2612. var key, value;
  2613. var index = -1;
  2614. var size = keys.length;
  2615. if (iterator.length === 3) {
  2616. while (++index < size) {
  2617. key = keys[index];
  2618. value = object[key];
  2619. iterator(value, key, createCallback(index, value));
  2620. }
  2621. } else {
  2622. while (++index < size) {
  2623. value = object[keys[index]];
  2624. iterator(value, createCallback(index, value));
  2625. }
  2626. }
  2627. }
  2628. /**
  2629. * @private
  2630. */
  2631. function symbolEachIndexValue(collection, iterator, createCallback) {
  2632. var value, item;
  2633. var index = -1;
  2634. var iter = collection[iteratorSymbol]();
  2635. if (iterator.length === 3) {
  2636. while ((item = iter.next()).done === false) {
  2637. value = item.value;
  2638. iterator(value, ++index, createCallback(index, value));
  2639. }
  2640. } else {
  2641. while ((item = iter.next()).done === false) {
  2642. value = item.value;
  2643. iterator(value, createCallback(++index, value));
  2644. }
  2645. }
  2646. }
  2647. /**
  2648. * @private
  2649. */
  2650. function baseEachKeyValue(object, iterator, createCallback, keys) {
  2651. var key, value;
  2652. var index = -1;
  2653. var size = keys.length;
  2654. if (iterator.length === 3) {
  2655. while (++index < size) {
  2656. key = keys[index];
  2657. value = object[key];
  2658. iterator(value, key, createCallback(key, value));
  2659. }
  2660. } else {
  2661. while (++index < size) {
  2662. key = keys[index];
  2663. value = object[key];
  2664. iterator(value, createCallback(key, value));
  2665. }
  2666. }
  2667. }
  2668. /**
  2669. * @private
  2670. */
  2671. function symbolEachKeyValue(collection, iterator, createCallback) {
  2672. var value, item;
  2673. var index = -1;
  2674. var iter = collection[iteratorSymbol]();
  2675. if (iterator.length === 3) {
  2676. while ((item = iter.next()).done === false) {
  2677. value = item.value;
  2678. iterator(value, ++index, createCallback(index, value));
  2679. }
  2680. } else {
  2681. while ((item = iter.next()).done === false) {
  2682. value = item.value;
  2683. iterator(value, createCallback(++index, value));
  2684. }
  2685. }
  2686. }
  2687. /**
  2688. * @private
  2689. * @param {Function} func
  2690. */
  2691. function onlyOnce(func) {
  2692. return function(err, res) {
  2693. var fn = func;
  2694. func = throwError;
  2695. fn(err, res);
  2696. };
  2697. }
  2698. /**
  2699. * @private
  2700. * @param {Function} func
  2701. */
  2702. function once(func) {
  2703. return function(err, res) {
  2704. var fn = func;
  2705. func = noop;
  2706. fn(err, res);
  2707. };
  2708. }
  2709. /**
  2710. * @private
  2711. * @param {Function} arrayEach
  2712. * @param {Function} baseEach
  2713. */
  2714. function createEach(arrayEach, baseEach, symbolEach) {
  2715. return function each(collection, iterator, callback) {
  2716. callback = callback || noop;
  2717. var size, keys;
  2718. var completed = 0;
  2719. if (isArray(collection)) {
  2720. size = collection.length;
  2721. arrayEach(collection, iterator, done);
  2722. } else if (!collection) {
  2723. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  2724. size = collection.size;
  2725. symbolEach(collection, iterator, done);
  2726. } else if (typeof collection === obj) {
  2727. keys = nativeKeys(collection);
  2728. size = keys.length;
  2729. baseEach(collection, iterator, done, keys);
  2730. }
  2731. if (!size) {
  2732. callback(null);
  2733. }
  2734. function done(err, bool) {
  2735. if (err) {
  2736. callback = once(callback);
  2737. callback(err);
  2738. } else if (++completed === size) {
  2739. callback(null);
  2740. } else if (bool === false) {
  2741. callback = once(callback);
  2742. callback(null);
  2743. }
  2744. }
  2745. };
  2746. }
  2747. /**
  2748. * @private
  2749. * @param {Function} arrayEach
  2750. * @param {Function} baseEach
  2751. * @param {Function} symbolEach
  2752. */
  2753. function createMap(arrayEach, baseEach, symbolEach, useArray) {
  2754. var init, clone;
  2755. if (useArray) {
  2756. init = Array;
  2757. clone = createArray;
  2758. } else {
  2759. init = function() {
  2760. return {};
  2761. };
  2762. clone = objectClone;
  2763. }
  2764. return function(collection, iterator, callback) {
  2765. callback = callback || noop;
  2766. var size, keys, result;
  2767. var completed = 0;
  2768. if (isArray(collection)) {
  2769. size = collection.length;
  2770. result = init(size);
  2771. arrayEach(collection, iterator, createCallback);
  2772. } else if (!collection) {
  2773. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  2774. size = collection.size;
  2775. result = init(size);
  2776. symbolEach(collection, iterator, createCallback);
  2777. } else if (typeof collection === obj) {
  2778. keys = nativeKeys(collection);
  2779. size = keys.length;
  2780. result = init(size);
  2781. baseEach(collection, iterator, createCallback, keys);
  2782. }
  2783. if (!size) {
  2784. callback(null, init());
  2785. }
  2786. function createCallback(key) {
  2787. return function done(err, res) {
  2788. if (key === null) {
  2789. throwError();
  2790. }
  2791. if (err) {
  2792. key = null;
  2793. callback = once(callback);
  2794. callback(err, clone(result));
  2795. return;
  2796. }
  2797. result[key] = res;
  2798. key = null;
  2799. if (++completed === size) {
  2800. callback(null, result);
  2801. }
  2802. };
  2803. }
  2804. };
  2805. }
  2806. /**
  2807. * @private
  2808. * @param {Function} arrayEach
  2809. * @param {Function} baseEach
  2810. * @param {Function} symbolEach
  2811. * @param {boolean} bool
  2812. */
  2813. function createFilter(arrayEach, baseEach, symbolEach, bool) {
  2814. return function(collection, iterator, callback) {
  2815. callback = callback || noop;
  2816. var size, keys, result;
  2817. var completed = 0;
  2818. if (isArray(collection)) {
  2819. size = collection.length;
  2820. result = Array(size);
  2821. arrayEach(collection, iterator, createCallback);
  2822. } else if (!collection) {
  2823. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  2824. size = collection.size;
  2825. result = Array(size);
  2826. symbolEach(collection, iterator, createCallback);
  2827. } else if (typeof collection === obj) {
  2828. keys = nativeKeys(collection);
  2829. size = keys.length;
  2830. result = Array(size);
  2831. baseEach(collection, iterator, createCallback, keys);
  2832. }
  2833. if (!size) {
  2834. return callback(null, []);
  2835. }
  2836. function createCallback(index, value) {
  2837. return function done(err, res) {
  2838. if (index === null) {
  2839. throwError();
  2840. }
  2841. if (err) {
  2842. index = null;
  2843. callback = once(callback);
  2844. callback(err);
  2845. return;
  2846. }
  2847. if (!!res === bool) {
  2848. result[index] = value;
  2849. }
  2850. index = null;
  2851. if (++completed === size) {
  2852. callback(null, compact(result));
  2853. }
  2854. };
  2855. }
  2856. };
  2857. }
  2858. /**
  2859. * @private
  2860. * @param {boolean} bool
  2861. */
  2862. function createFilterSeries(bool) {
  2863. return function(collection, iterator, callback) {
  2864. callback = onlyOnce(callback || noop);
  2865. var size, key, value, keys, iter, iterate;
  2866. var sync = false;
  2867. var completed = 0;
  2868. var result = [];
  2869. if (isArray(collection)) {
  2870. size = collection.length;
  2871. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  2872. } else if (!collection) {
  2873. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  2874. size = collection.size;
  2875. iter = collection[iteratorSymbol]();
  2876. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  2877. } else if (typeof collection === obj) {
  2878. keys = nativeKeys(collection);
  2879. size = keys.length;
  2880. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  2881. }
  2882. if (!size) {
  2883. return callback(null, []);
  2884. }
  2885. iterate();
  2886. function arrayIterator() {
  2887. value = collection[completed];
  2888. iterator(value, done);
  2889. }
  2890. function arrayIteratorWithIndex() {
  2891. value = collection[completed];
  2892. iterator(value, completed, done);
  2893. }
  2894. function symbolIterator() {
  2895. value = iter.next().value;
  2896. iterator(value, done);
  2897. }
  2898. function symbolIteratorWithKey() {
  2899. value = iter.next().value;
  2900. iterator(value, completed, done);
  2901. }
  2902. function objectIterator() {
  2903. key = keys[completed];
  2904. value = collection[key];
  2905. iterator(value, done);
  2906. }
  2907. function objectIteratorWithKey() {
  2908. key = keys[completed];
  2909. value = collection[key];
  2910. iterator(value, key, done);
  2911. }
  2912. function done(err, res) {
  2913. if (err) {
  2914. callback(err);
  2915. return;
  2916. }
  2917. if (!!res === bool) {
  2918. result[result.length] = value;
  2919. }
  2920. if (++completed === size) {
  2921. iterate = throwError;
  2922. callback(null, result);
  2923. } else if (sync) {
  2924. nextTick(iterate);
  2925. } else {
  2926. sync = true;
  2927. iterate();
  2928. }
  2929. sync = false;
  2930. }
  2931. };
  2932. }
  2933. /**
  2934. * @private
  2935. * @param {boolean} bool
  2936. */
  2937. function createFilterLimit(bool) {
  2938. return function(collection, limit, iterator, callback) {
  2939. callback = callback || noop;
  2940. var size, index, key, value, keys, iter, item, iterate, result;
  2941. var sync = false;
  2942. var started = 0;
  2943. var completed = 0;
  2944. if (isArray(collection)) {
  2945. size = collection.length;
  2946. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  2947. } else if (!collection) {
  2948. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  2949. size = collection.size;
  2950. iter = collection[iteratorSymbol]();
  2951. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  2952. } else if (typeof collection === obj) {
  2953. keys = nativeKeys(collection);
  2954. size = keys.length;
  2955. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  2956. }
  2957. if (!size || isNaN(limit) || limit < 1) {
  2958. return callback(null, []);
  2959. }
  2960. result = Array(size);
  2961. timesSync(limit > size ? size : limit, iterate);
  2962. function arrayIterator() {
  2963. index = started++;
  2964. if (index < size) {
  2965. value = collection[index];
  2966. iterator(value, createCallback(value, index));
  2967. }
  2968. }
  2969. function arrayIteratorWithIndex() {
  2970. index = started++;
  2971. if (index < size) {
  2972. value = collection[index];
  2973. iterator(value, index, createCallback(value, index));
  2974. }
  2975. }
  2976. function symbolIterator() {
  2977. if ((item = iter.next()).done === false) {
  2978. value = item.value;
  2979. iterator(value, createCallback(value, started++));
  2980. }
  2981. }
  2982. function symbolIteratorWithKey() {
  2983. if ((item = iter.next()).done === false) {
  2984. value = item.value;
  2985. iterator(value, started, createCallback(value, started++));
  2986. }
  2987. }
  2988. function objectIterator() {
  2989. index = started++;
  2990. if (index < size) {
  2991. value = collection[keys[index]];
  2992. iterator(value, createCallback(value, index));
  2993. }
  2994. }
  2995. function objectIteratorWithKey() {
  2996. index = started++;
  2997. if (index < size) {
  2998. key = keys[index];
  2999. value = collection[key];
  3000. iterator(value, key, createCallback(value, index));
  3001. }
  3002. }
  3003. function createCallback(value, index) {
  3004. return function(err, res) {
  3005. if (index === null) {
  3006. throwError();
  3007. }
  3008. if (err) {
  3009. index = null;
  3010. iterate = noop;
  3011. callback = once(callback);
  3012. callback(err);
  3013. return;
  3014. }
  3015. if (!!res === bool) {
  3016. result[index] = value;
  3017. }
  3018. index = null;
  3019. if (++completed === size) {
  3020. callback = onlyOnce(callback);
  3021. callback(null, compact(result));
  3022. } else if (sync) {
  3023. nextTick(iterate);
  3024. } else {
  3025. sync = true;
  3026. iterate();
  3027. }
  3028. sync = false;
  3029. };
  3030. }
  3031. };
  3032. }
  3033. /**
  3034. * @memberof async
  3035. * @namespace eachSeries
  3036. * @param {Array|Object} collection
  3037. * @param {Function} iterator
  3038. * @param {Function} callback
  3039. * @example
  3040. *
  3041. * // array
  3042. * var order = [];
  3043. * var array = [1, 3, 2];
  3044. * var iterator = function(num, done) {
  3045. * setTimeout(function() {
  3046. * order.push(num);
  3047. * done();
  3048. * }, num * 10);
  3049. * };
  3050. * async.eachSeries(array, iterator, function(err, res) {
  3051. * console.log(res); // undefined
  3052. * console.log(order); // [1, 3, 2]
  3053. * });
  3054. *
  3055. * @example
  3056. *
  3057. * // array with index
  3058. * var order = [];
  3059. * var array = [1, 3, 2];
  3060. * var iterator = function(num, index, done) {
  3061. * setTimeout(function() {
  3062. * order.push([num, index]);
  3063. * done();
  3064. * }, num * 10);
  3065. * };
  3066. * async.eachSeries(array, iterator, function(err, res) {
  3067. * console.log(res); // undefined
  3068. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  3069. * });
  3070. *
  3071. * @example
  3072. *
  3073. * // object
  3074. * var order = [];
  3075. * var object = { a: 1, b: 3, c: 2 };
  3076. * var iterator = function(num, done) {
  3077. * setTimeout(function() {
  3078. * order.push(num);
  3079. * done();
  3080. * }, num * 10);
  3081. * };
  3082. * async.eachSeries(object, iterator, function(err, res) {
  3083. * console.log(res); // undefined
  3084. * console.log(order); // [1, 3, 2]
  3085. * });
  3086. *
  3087. * @example
  3088. *
  3089. * // object with key
  3090. * var order = [];
  3091. * var object = { a: 1, b: 3, c: 2 };
  3092. * var iterator = function(num, key, done) {
  3093. * setTimeout(function() {
  3094. * order.push([num, key]);
  3095. * done();
  3096. * }, num * 10);
  3097. * };
  3098. * async.eachSeries(object, iterator, function(err, res) {
  3099. * console.log(res); // undefined
  3100. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b']]
  3101. * });
  3102. *
  3103. * @example
  3104. *
  3105. * // break
  3106. * var order = [];
  3107. * var array = [1, 3, 2];
  3108. * var iterator = function(num, done) {
  3109. * setTimeout(function() {
  3110. * order.push(num);
  3111. * done(null, num !== 3);
  3112. * }, num * 10);
  3113. * };
  3114. * async.eachSeries(array, iterator, function(err, res) {
  3115. * console.log(res); // undefined
  3116. * console.log(order); // [1, 3]
  3117. * });
  3118. */
  3119. function eachSeries(collection, iterator, callback) {
  3120. callback = onlyOnce(callback || noop);
  3121. var size, key, keys, iter, value, iterate;
  3122. var sync = false;
  3123. var completed = 0;
  3124. if (isArray(collection)) {
  3125. size = collection.length;
  3126. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  3127. } else if (!collection) {
  3128. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3129. size = collection.size;
  3130. iter = collection[iteratorSymbol]();
  3131. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  3132. } else if (typeof collection === obj) {
  3133. keys = nativeKeys(collection);
  3134. size = keys.length;
  3135. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  3136. }
  3137. if (!size) {
  3138. return callback(null);
  3139. }
  3140. iterate();
  3141. function arrayIterator() {
  3142. iterator(collection[completed], done);
  3143. }
  3144. function arrayIteratorWithIndex() {
  3145. iterator(collection[completed], completed, done);
  3146. }
  3147. function symbolIterator() {
  3148. iterator(iter.next().value, done);
  3149. }
  3150. function symbolIteratorWithKey() {
  3151. value = iter.next().value;
  3152. iterator(value, completed, done);
  3153. }
  3154. function objectIterator() {
  3155. iterator(collection[keys[completed]], done);
  3156. }
  3157. function objectIteratorWithKey() {
  3158. key = keys[completed];
  3159. iterator(collection[key], key, done);
  3160. }
  3161. function done(err, bool) {
  3162. if (err) {
  3163. callback(err);
  3164. } else if (++completed === size) {
  3165. iterate = throwError;
  3166. callback(null);
  3167. } else if (bool === false) {
  3168. iterate = throwError;
  3169. callback(null);
  3170. } else if (sync) {
  3171. nextTick(iterate);
  3172. } else {
  3173. sync = true;
  3174. iterate();
  3175. }
  3176. sync = false;
  3177. }
  3178. }
  3179. /**
  3180. * @memberof async
  3181. * @namespace eachLimit
  3182. * @param {Array|Object} collection
  3183. * @param {number} limit - limit >= 1
  3184. * @param {Function} iterator
  3185. * @param {Function} callback
  3186. * @example
  3187. *
  3188. * // array
  3189. * var order = [];
  3190. * var array = [1, 5, 3, 4, 2];
  3191. * var iterator = function(num, done) {
  3192. * setTimeout(function() {
  3193. * order.push(num);
  3194. * done();
  3195. * }, num * 10);
  3196. * };
  3197. * async.eachLimit(array, 2, iterator, function(err, res) {
  3198. * console.log(res); // undefined
  3199. * console.log(order); // [1, 3, 5, 2, 4]
  3200. * });
  3201. *
  3202. * @example
  3203. *
  3204. * // array with index
  3205. * var order = [];
  3206. * var array = [1, 5, 3, 4, 2];
  3207. * var iterator = function(num, index, done) {
  3208. * setTimeout(function() {
  3209. * order.push([num, index]);
  3210. * done();
  3211. * }, num * 10);
  3212. * };
  3213. * async.eachLimit(array, 2, iterator, function(err, res) {
  3214. * console.log(res); // undefined
  3215. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  3216. * });
  3217. *
  3218. * @example
  3219. *
  3220. * // object
  3221. * var order = [];
  3222. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  3223. * var iterator = function(num, done) {
  3224. * setTimeout(function() {
  3225. * order.push(num);
  3226. * done();
  3227. * }, num * 10);
  3228. * };
  3229. * async.eachLimit(object, 2, iterator, function(err, res) {
  3230. * console.log(res); // undefined
  3231. * console.log(order); // [1, 3, 5, 2, 4]
  3232. * });
  3233. *
  3234. * @example
  3235. *
  3236. * // object with key
  3237. * var order = [];
  3238. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  3239. * var iterator = function(num, key, done) {
  3240. * setTimeout(function() {
  3241. * order.push([num, key]);
  3242. * done();
  3243. * }, num * 10);
  3244. * };
  3245. * async.eachLimit(object, 2, iterator, function(err, res) {
  3246. * console.log(res); // undefined
  3247. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  3248. * });
  3249. *
  3250. * @example
  3251. *
  3252. * // break
  3253. * var order = [];
  3254. * var array = [1, 5, 3, 4, 2];
  3255. * var iterator = function(num, done) {
  3256. * setTimeout(function() {
  3257. * order.push(num);
  3258. * done(null, num !== 5);
  3259. * }, num * 10);
  3260. * };
  3261. * async.eachLimit(array, 2, iterator, function(err, res) {
  3262. * console.log(res); // undefined
  3263. * console.log(order); // [1, 3, 5]
  3264. * });
  3265. *
  3266. */
  3267. function eachLimit(collection, limit, iterator, callback) {
  3268. callback = callback || noop;
  3269. var size, index, key, keys, iter, item, iterate;
  3270. var sync = false;
  3271. var started = 0;
  3272. var completed = 0;
  3273. if (isArray(collection)) {
  3274. size = collection.length;
  3275. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  3276. } else if (!collection) {
  3277. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3278. size = collection.size;
  3279. iter = collection[iteratorSymbol]();
  3280. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  3281. } else if (typeof collection === obj) {
  3282. keys = nativeKeys(collection);
  3283. size = keys.length;
  3284. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  3285. } else {
  3286. return callback(null);
  3287. }
  3288. if (!size || isNaN(limit) || limit < 1) {
  3289. return callback(null);
  3290. }
  3291. timesSync(limit > size ? size : limit, iterate);
  3292. function arrayIterator() {
  3293. if (started < size) {
  3294. iterator(collection[started++], done);
  3295. }
  3296. }
  3297. function arrayIteratorWithIndex() {
  3298. index = started++;
  3299. if (index < size) {
  3300. iterator(collection[index], index, done);
  3301. }
  3302. }
  3303. function symbolIterator() {
  3304. if ((item = iter.next()).done === false) {
  3305. iterator(item.value, done);
  3306. }
  3307. }
  3308. function symbolIteratorWithKey() {
  3309. if ((item = iter.next()).done === false) {
  3310. iterator(item.value, started++, done);
  3311. }
  3312. }
  3313. function objectIterator() {
  3314. if (started < size) {
  3315. iterator(collection[keys[started++]], done);
  3316. }
  3317. }
  3318. function objectIteratorWithKey() {
  3319. index = started++;
  3320. if (index < size) {
  3321. key = keys[index];
  3322. iterator(collection[key], key, done);
  3323. }
  3324. }
  3325. function done(err, bool) {
  3326. if (err) {
  3327. iterate = noop;
  3328. callback = once(callback);
  3329. callback(err);
  3330. } else if (++completed === size) {
  3331. iterate = throwError;
  3332. callback = onlyOnce(callback);
  3333. callback(null);
  3334. } else if (bool === false) {
  3335. iterate = noop;
  3336. callback = once(callback);
  3337. callback(null);
  3338. } else if (sync) {
  3339. nextTick(iterate);
  3340. } else {
  3341. sync = true;
  3342. iterate();
  3343. }
  3344. sync = false;
  3345. }
  3346. }
  3347. /**
  3348. * @memberof async
  3349. * @namespace mapSeries
  3350. * @param {Array|Object} collection
  3351. * @param {Function} iterator
  3352. * @param {Function} callback
  3353. * @example
  3354. *
  3355. * // array
  3356. * var order = [];
  3357. * var array = [1, 3, 2];
  3358. * var iterator = function(num, done) {
  3359. * setTimeout(function() {
  3360. * order.push(num);
  3361. * done(null, num);
  3362. * }, num * 10);
  3363. * };
  3364. * async.mapSeries(array, iterator, function(err, res) {
  3365. * console.log(res); // [1, 3, 2];
  3366. * console.log(order); // [1, 3, 2]
  3367. * });
  3368. *
  3369. * @example
  3370. *
  3371. * // array with index
  3372. * var order = [];
  3373. * var array = [1, 3, 2];
  3374. * var iterator = function(num, index, done) {
  3375. * setTimeout(function() {
  3376. * order.push([num, index]);
  3377. * done(null, num);
  3378. * }, num * 10);
  3379. * };
  3380. * async.mapSeries(array, iterator, function(err, res) {
  3381. * console.log(res); // [1, 3, 2]
  3382. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  3383. * });
  3384. *
  3385. * @example
  3386. *
  3387. * // object
  3388. * var order = [];
  3389. * var object = { a: 1, b: 3, c: 2 };
  3390. * var iterator = function(num, done) {
  3391. * setTimeout(function() {
  3392. * order.push(num);
  3393. * done(null, num);
  3394. * }, num * 10);
  3395. * };
  3396. * async.mapSeries(object, iterator, function(err, res) {
  3397. * console.log(res); // [1, 3, 2]
  3398. * console.log(order); // [1, 3, 2]
  3399. * });
  3400. *
  3401. * @example
  3402. *
  3403. * // object with key
  3404. * var order = [];
  3405. * var object = { a: 1, b: 3, c: 2 };
  3406. * var iterator = function(num, key, done) {
  3407. * setTimeout(function() {
  3408. * order.push([num, key]);
  3409. * done(null, num);
  3410. * }, num * 10);
  3411. * };
  3412. * async.mapSeries(object, iterator, function(err, res) {
  3413. * console.log(res); // [1, 3, 2]
  3414. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
  3415. * });
  3416. *
  3417. */
  3418. function mapSeries(collection, iterator, callback) {
  3419. callback = callback || noop;
  3420. var size, key, keys, iter, value, result, iterate;
  3421. var sync = false;
  3422. var completed = 0;
  3423. if (isArray(collection)) {
  3424. size = collection.length;
  3425. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  3426. } else if (!collection) {
  3427. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3428. size = collection.size;
  3429. iter = collection[iteratorSymbol]();
  3430. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  3431. } else if (typeof collection === obj) {
  3432. keys = nativeKeys(collection);
  3433. size = keys.length;
  3434. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  3435. }
  3436. if (!size) {
  3437. return callback(null, []);
  3438. }
  3439. result = Array(size);
  3440. iterate();
  3441. function arrayIterator() {
  3442. iterator(collection[completed], done);
  3443. }
  3444. function arrayIteratorWithIndex() {
  3445. iterator(collection[completed], completed, done);
  3446. }
  3447. function symbolIterator() {
  3448. iterator(iter.next().value, done);
  3449. }
  3450. function symbolIteratorWithKey() {
  3451. value = iter.next().value;
  3452. iterator(value, completed, done);
  3453. }
  3454. function objectIterator() {
  3455. iterator(collection[keys[completed]], done);
  3456. }
  3457. function objectIteratorWithKey() {
  3458. key = keys[completed];
  3459. iterator(collection[key], key, done);
  3460. }
  3461. function done(err, res) {
  3462. if (err) {
  3463. iterate = throwError;
  3464. callback = onlyOnce(callback);
  3465. callback(err, createArray(result));
  3466. return;
  3467. }
  3468. result[completed] = res;
  3469. if (++completed === size) {
  3470. iterate = throwError;
  3471. callback(null, result);
  3472. callback = throwError;
  3473. } else if (sync) {
  3474. nextTick(iterate);
  3475. } else {
  3476. sync = true;
  3477. iterate();
  3478. }
  3479. sync = false;
  3480. }
  3481. }
  3482. /**
  3483. * @memberof async
  3484. * @namespace mapLimit
  3485. * @param {Array|Object} collection
  3486. * @param {number} limit - limit >= 1
  3487. * @param {Function} iterator
  3488. * @param {Function} callback
  3489. * @example
  3490. *
  3491. * // array
  3492. * var order = [];
  3493. * var array = [1, 5, 3, 4, 2];
  3494. * var iterator = function(num, done) {
  3495. * setTimeout(function() {
  3496. * order.push(num);
  3497. * done(null, num);
  3498. * }, num * 10);
  3499. * };
  3500. * async.mapLimit(array, 2, iterator, function(err, res) {
  3501. * console.log(res); // [1, 5, 3, 4, 2]
  3502. * console.log(order); // [1, 3, 5, 2, 4]
  3503. * });
  3504. *
  3505. * @example
  3506. *
  3507. * // array with index
  3508. * var order = [];
  3509. * var array = [1, 5, 3, 4, 2];
  3510. * var iterator = function(num, index, done) {
  3511. * setTimeout(function() {
  3512. * order.push([num, index]);
  3513. * done(null, num);
  3514. * }, num * 10);
  3515. * };
  3516. * async.mapLimit(array, 2, iterator, function(err, res) {
  3517. * console.log(res); // [1, 5, 3, 4, 2]
  3518. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  3519. * });
  3520. *
  3521. * @example
  3522. *
  3523. * // object
  3524. * var order = [];
  3525. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  3526. * var iterator = function(num, done) {
  3527. * setTimeout(function() {
  3528. * order.push(num);
  3529. * done(null, num);
  3530. * }, num * 10);
  3531. * };
  3532. * async.mapLimit(object, 2, iterator, function(err, res) {
  3533. * console.log(res); // [1, 5, 3, 4, 2]
  3534. * console.log(order); // [1, 3, 5, 2, 4]
  3535. * });
  3536. *
  3537. * @example
  3538. *
  3539. * // object with key
  3540. * var order = [];
  3541. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  3542. * var iterator = function(num, key, done) {
  3543. * setTimeout(function() {
  3544. * order.push([num, key]);
  3545. * done(null, num);
  3546. * }, num * 10);
  3547. * };
  3548. * async.mapLimit(object, 2, iterator, function(err, res) {
  3549. * console.log(res); // [1, 5, 3, 4, 2]
  3550. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  3551. * });
  3552. *
  3553. */
  3554. function mapLimit(collection, limit, iterator, callback) {
  3555. callback = callback || noop;
  3556. var size, index, key, keys, iter, item, result, iterate;
  3557. var sync = false;
  3558. var started = 0;
  3559. var completed = 0;
  3560. if (isArray(collection)) {
  3561. size = collection.length;
  3562. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  3563. } else if (!collection) {
  3564. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3565. size = collection.size;
  3566. iter = collection[iteratorSymbol]();
  3567. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  3568. } else if (typeof collection === obj) {
  3569. keys = nativeKeys(collection);
  3570. size = keys.length;
  3571. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  3572. }
  3573. if (!size || isNaN(limit) || limit < 1) {
  3574. return callback(null, []);
  3575. }
  3576. result = Array(size);
  3577. timesSync(limit > size ? size : limit, iterate);
  3578. function arrayIterator() {
  3579. index = started++;
  3580. if (index < size) {
  3581. iterator(collection[index], createCallback(index));
  3582. }
  3583. }
  3584. function arrayIteratorWithIndex() {
  3585. index = started++;
  3586. if (index < size) {
  3587. iterator(collection[index], index, createCallback(index));
  3588. }
  3589. }
  3590. function symbolIterator() {
  3591. if ((item = iter.next()).done === false) {
  3592. iterator(item.value, createCallback(started++));
  3593. }
  3594. }
  3595. function symbolIteratorWithKey() {
  3596. if ((item = iter.next()).done === false) {
  3597. iterator(item.value, started, createCallback(started++));
  3598. }
  3599. }
  3600. function objectIterator() {
  3601. index = started++;
  3602. if (index < size) {
  3603. iterator(collection[keys[index]], createCallback(index));
  3604. }
  3605. }
  3606. function objectIteratorWithKey() {
  3607. index = started++;
  3608. if (index < size) {
  3609. key = keys[index];
  3610. iterator(collection[key], key, createCallback(index));
  3611. }
  3612. }
  3613. function createCallback(index) {
  3614. return function(err, res) {
  3615. if (index === null) {
  3616. throwError();
  3617. }
  3618. if (err) {
  3619. index = null;
  3620. iterate = noop;
  3621. callback = once(callback);
  3622. callback(err, createArray(result));
  3623. return;
  3624. }
  3625. result[index] = res;
  3626. index = null;
  3627. if (++completed === size) {
  3628. iterate = throwError;
  3629. callback(null, result);
  3630. callback = throwError;
  3631. } else if (sync) {
  3632. nextTick(iterate);
  3633. } else {
  3634. sync = true;
  3635. iterate();
  3636. }
  3637. sync = false;
  3638. };
  3639. }
  3640. }
  3641. /**
  3642. * @memberof async
  3643. * @namespace mapValuesSeries
  3644. * @param {Array|Object} collection
  3645. * @param {Function} iterator
  3646. * @param {Function} callback
  3647. * @example
  3648. *
  3649. * // array
  3650. * var order = [];
  3651. * var array = [1, 3, 2];
  3652. * var iterator = function(num, done) {
  3653. * setTimeout(function() {
  3654. * order.push(num);
  3655. * done(null, num);
  3656. * }, num * 10);
  3657. * };
  3658. * async.mapValuesSeries(array, iterator, function(err, res) {
  3659. * console.log(res); // { '0': 1, '1': 3, '2': 2 }
  3660. * console.log(order); // [1, 3, 2]
  3661. * });
  3662. *
  3663. * @example
  3664. *
  3665. * // array with index
  3666. * var order = [];
  3667. * var array = [1, 3, 2];
  3668. * var iterator = function(num, index, done) {
  3669. * setTimeout(function() {
  3670. * order.push([num, index]);
  3671. * done(null, num);
  3672. * }, num * 10);
  3673. * };
  3674. * async.mapValuesSeries(array, iterator, function(err, res) {
  3675. * console.log(res); // { '0': 1, '1': 3, '2': 2 }
  3676. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  3677. * });
  3678. *
  3679. * @example
  3680. *
  3681. * // object
  3682. * var order = [];
  3683. * var object = { a: 1, b: 3, c: 2 };
  3684. * var iterator = function(num, done) {
  3685. * setTimeout(function() {
  3686. * order.push(num);
  3687. * done(null, num);
  3688. * }, num * 10);
  3689. * };
  3690. * async.mapValuesSeries(object, iterator, function(err, res) {
  3691. * console.log(res); // { a: 1, b: 3, c: 2 }
  3692. * console.log(order); // [1, 3, 2]
  3693. * });
  3694. *
  3695. * @example
  3696. *
  3697. * // object with key
  3698. * var order = [];
  3699. * var object = { a: 1, b: 3, c: 2 };
  3700. * var iterator = function(num, key, done) {
  3701. * setTimeout(function() {
  3702. * order.push([num, key]);
  3703. * done(null, num);
  3704. * }, num * 10);
  3705. * };
  3706. * async.mapValuesSeries(object, iterator, function(err, res) {
  3707. * console.log(res); // { a: 1, b: 3, c: 2 }
  3708. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
  3709. * });
  3710. *
  3711. */
  3712. function mapValuesSeries(collection, iterator, callback) {
  3713. callback = callback || noop;
  3714. var size, key, keys, iter, value, iterate;
  3715. var sync = false;
  3716. var result = {};
  3717. var completed = 0;
  3718. if (isArray(collection)) {
  3719. size = collection.length;
  3720. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  3721. } else if (!collection) {
  3722. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3723. size = collection.size;
  3724. iter = collection[iteratorSymbol]();
  3725. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  3726. } else if (typeof collection === obj) {
  3727. keys = nativeKeys(collection);
  3728. size = keys.length;
  3729. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  3730. }
  3731. if (!size) {
  3732. return callback(null, result);
  3733. }
  3734. iterate();
  3735. function arrayIterator() {
  3736. key = completed;
  3737. iterator(collection[completed], done);
  3738. }
  3739. function arrayIteratorWithIndex() {
  3740. key = completed;
  3741. iterator(collection[completed], completed, done);
  3742. }
  3743. function symbolIterator() {
  3744. key = completed;
  3745. value = iter.next().value;
  3746. iterator(value, done);
  3747. }
  3748. function symbolIteratorWithKey() {
  3749. key = completed;
  3750. value = iter.next().value;
  3751. iterator(value, completed, done);
  3752. }
  3753. function objectIterator() {
  3754. key = keys[completed];
  3755. iterator(collection[key], done);
  3756. }
  3757. function objectIteratorWithKey() {
  3758. key = keys[completed];
  3759. iterator(collection[key], key, done);
  3760. }
  3761. function done(err, res) {
  3762. if (err) {
  3763. iterate = throwError;
  3764. callback = onlyOnce(callback);
  3765. callback(err, objectClone(result));
  3766. return;
  3767. }
  3768. result[key] = res;
  3769. if (++completed === size) {
  3770. iterate = throwError;
  3771. callback(null, result);
  3772. callback = throwError;
  3773. } else if (sync) {
  3774. nextTick(iterate);
  3775. } else {
  3776. sync = true;
  3777. iterate();
  3778. }
  3779. sync = false;
  3780. }
  3781. }
  3782. /**
  3783. * @memberof async
  3784. * @namespace mapValuesLimit
  3785. * @param {Array|Object} collection
  3786. * @param {number} limit - limit >= 1
  3787. * @param {Function} iterator
  3788. * @param {Function} callback
  3789. * @example
  3790. *
  3791. * // array
  3792. * var order = [];
  3793. * var array = [1, 5, 3, 4, 2];
  3794. * var iterator = function(num, done) {
  3795. * setTimeout(function() {
  3796. * order.push(num);
  3797. * done(null, num);
  3798. * }, num * 10);
  3799. * };
  3800. * async.mapValuesLimit(array, 2, iterator, function(err, res) {
  3801. * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
  3802. * console.log(order); // [1, 3, 5, 2, 4]
  3803. * });
  3804. *
  3805. * @example
  3806. *
  3807. * // array with index
  3808. * var order = [];
  3809. * var array = [1, 5, 3, 4, 2];
  3810. * var iterator = function(num, index, done) {
  3811. * setTimeout(function() {
  3812. * order.push([num, index]);
  3813. * done(null, num);
  3814. * }, num * 10);
  3815. * };
  3816. * async.mapValuesLimit(array, 2, iterator, function(err, res) {
  3817. * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
  3818. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  3819. * });
  3820. *
  3821. * @example
  3822. *
  3823. * // object
  3824. * var order = [];
  3825. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  3826. * var iterator = function(num, done) {
  3827. * setTimeout(function() {
  3828. * order.push(num);
  3829. * done(null, num);
  3830. * }, num * 10);
  3831. * };
  3832. * async.mapValuesLimit(object, 2, iterator, function(err, res) {
  3833. * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 }
  3834. * console.log(order); // [1, 3, 5, 2, 4]
  3835. * });
  3836. *
  3837. * @example
  3838. *
  3839. * // object with key
  3840. * var order = [];
  3841. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  3842. * var iterator = function(num, key, done) {
  3843. * setTimeout(function() {
  3844. * order.push([num, key]);
  3845. * done(null, num);
  3846. * }, num * 10);
  3847. * };
  3848. * async.mapValuesLimit(object, 2, iterator, function(err, res) {
  3849. * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 }
  3850. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  3851. * });
  3852. *
  3853. */
  3854. function mapValuesLimit(collection, limit, iterator, callback) {
  3855. callback = callback || noop;
  3856. var size, index, key, keys, iter, item, iterate;
  3857. var sync = false;
  3858. var result = {};
  3859. var started = 0;
  3860. var completed = 0;
  3861. if (isArray(collection)) {
  3862. size = collection.length;
  3863. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  3864. } else if (!collection) {
  3865. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3866. size = collection.size;
  3867. iter = collection[iteratorSymbol]();
  3868. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  3869. } else if (typeof collection === obj) {
  3870. keys = nativeKeys(collection);
  3871. size = keys.length;
  3872. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  3873. }
  3874. if (!size || isNaN(limit) || limit < 1) {
  3875. return callback(null, result);
  3876. }
  3877. timesSync(limit > size ? size : limit, iterate);
  3878. function arrayIterator() {
  3879. index = started++;
  3880. if (index < size) {
  3881. iterator(collection[index], createCallback(index));
  3882. }
  3883. }
  3884. function arrayIteratorWithIndex() {
  3885. index = started++;
  3886. if (index < size) {
  3887. iterator(collection[index], index, createCallback(index));
  3888. }
  3889. }
  3890. function symbolIterator() {
  3891. if ((item = iter.next()).done === false) {
  3892. iterator(item.value, createCallback(started++));
  3893. }
  3894. }
  3895. function symbolIteratorWithKey() {
  3896. if ((item = iter.next()).done === false) {
  3897. iterator(item.value, started, createCallback(started++));
  3898. }
  3899. }
  3900. function objectIterator() {
  3901. index = started++;
  3902. if (index < size) {
  3903. key = keys[index];
  3904. iterator(collection[key], createCallback(key));
  3905. }
  3906. }
  3907. function objectIteratorWithKey() {
  3908. index = started++;
  3909. if (index < size) {
  3910. key = keys[index];
  3911. iterator(collection[key], key, createCallback(key));
  3912. }
  3913. }
  3914. function createCallback(key) {
  3915. return function(err, res) {
  3916. if (key === null) {
  3917. throwError();
  3918. }
  3919. if (err) {
  3920. key = null;
  3921. iterate = noop;
  3922. callback = once(callback);
  3923. callback(err, objectClone(result));
  3924. return;
  3925. }
  3926. result[key] = res;
  3927. key = null;
  3928. if (++completed === size) {
  3929. callback(null, result);
  3930. } else if (sync) {
  3931. nextTick(iterate);
  3932. } else {
  3933. sync = true;
  3934. iterate();
  3935. }
  3936. sync = false;
  3937. };
  3938. }
  3939. }
  3940. /**
  3941. * @private
  3942. * @param {Function} arrayEach
  3943. * @param {Function} baseEach
  3944. * @param {Function} symbolEach
  3945. * @param {boolean} bool
  3946. */
  3947. function createDetect(arrayEach, baseEach, symbolEach, bool) {
  3948. return function(collection, iterator, callback) {
  3949. callback = callback || noop;
  3950. var size, keys;
  3951. var completed = 0;
  3952. if (isArray(collection)) {
  3953. size = collection.length;
  3954. arrayEach(collection, iterator, createCallback);
  3955. } else if (!collection) {
  3956. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  3957. size = collection.size;
  3958. symbolEach(collection, iterator, createCallback);
  3959. } else if (typeof collection === obj) {
  3960. keys = nativeKeys(collection);
  3961. size = keys.length;
  3962. baseEach(collection, iterator, createCallback, keys);
  3963. }
  3964. if (!size) {
  3965. callback(null);
  3966. }
  3967. function createCallback(value) {
  3968. var called = false;
  3969. return function done(err, res) {
  3970. if (called) {
  3971. throwError();
  3972. }
  3973. called = true;
  3974. if (err) {
  3975. callback = once(callback);
  3976. callback(err);
  3977. } else if (!!res === bool) {
  3978. callback = once(callback);
  3979. callback(null, value);
  3980. } else if (++completed === size) {
  3981. callback(null);
  3982. }
  3983. };
  3984. }
  3985. };
  3986. }
  3987. /**
  3988. * @private
  3989. * @param {boolean} bool
  3990. */
  3991. function createDetectSeries(bool) {
  3992. return function(collection, iterator, callback) {
  3993. callback = onlyOnce(callback || noop);
  3994. var size, key, value, keys, iter, iterate;
  3995. var sync = false;
  3996. var completed = 0;
  3997. if (isArray(collection)) {
  3998. size = collection.length;
  3999. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  4000. } else if (!collection) {
  4001. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4002. size = collection.size;
  4003. iter = collection[iteratorSymbol]();
  4004. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  4005. } else if (typeof collection === obj) {
  4006. keys = nativeKeys(collection);
  4007. size = keys.length;
  4008. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  4009. }
  4010. if (!size) {
  4011. return callback(null);
  4012. }
  4013. iterate();
  4014. function arrayIterator() {
  4015. value = collection[completed];
  4016. iterator(value, done);
  4017. }
  4018. function arrayIteratorWithIndex() {
  4019. value = collection[completed];
  4020. iterator(value, completed, done);
  4021. }
  4022. function symbolIterator() {
  4023. value = iter.next().value;
  4024. iterator(value, done);
  4025. }
  4026. function symbolIteratorWithKey() {
  4027. value = iter.next().value;
  4028. iterator(value, completed, done);
  4029. }
  4030. function objectIterator() {
  4031. value = collection[keys[completed]];
  4032. iterator(value, done);
  4033. }
  4034. function objectIteratorWithKey() {
  4035. key = keys[completed];
  4036. value = collection[key];
  4037. iterator(value, key, done);
  4038. }
  4039. function done(err, res) {
  4040. if (err) {
  4041. callback(err);
  4042. } else if (!!res === bool) {
  4043. iterate = throwError;
  4044. callback(null, value);
  4045. } else if (++completed === size) {
  4046. iterate = throwError;
  4047. callback(null);
  4048. } else if (sync) {
  4049. nextTick(iterate);
  4050. } else {
  4051. sync = true;
  4052. iterate();
  4053. }
  4054. sync = false;
  4055. }
  4056. };
  4057. }
  4058. /**
  4059. * @private
  4060. * @param {boolean} bool
  4061. */
  4062. function createDetectLimit(bool) {
  4063. return function(collection, limit, iterator, callback) {
  4064. callback = callback || noop;
  4065. var size, index, key, value, keys, iter, item, iterate;
  4066. var sync = false;
  4067. var started = 0;
  4068. var completed = 0;
  4069. if (isArray(collection)) {
  4070. size = collection.length;
  4071. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  4072. } else if (!collection) {
  4073. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4074. size = collection.size;
  4075. iter = collection[iteratorSymbol]();
  4076. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  4077. } else if (typeof collection === obj) {
  4078. keys = nativeKeys(collection);
  4079. size = keys.length;
  4080. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  4081. }
  4082. if (!size || isNaN(limit) || limit < 1) {
  4083. return callback(null);
  4084. }
  4085. timesSync(limit > size ? size : limit, iterate);
  4086. function arrayIterator() {
  4087. index = started++;
  4088. if (index < size) {
  4089. value = collection[index];
  4090. iterator(value, createCallback(value));
  4091. }
  4092. }
  4093. function arrayIteratorWithIndex() {
  4094. index = started++;
  4095. if (index < size) {
  4096. value = collection[index];
  4097. iterator(value, index, createCallback(value));
  4098. }
  4099. }
  4100. function symbolIterator() {
  4101. if ((item = iter.next()).done === false) {
  4102. value = item.value;
  4103. iterator(value, createCallback(value));
  4104. }
  4105. }
  4106. function symbolIteratorWithKey() {
  4107. if ((item = iter.next()).done === false) {
  4108. value = item.value;
  4109. iterator(value, started++, createCallback(value));
  4110. }
  4111. }
  4112. function objectIterator() {
  4113. index = started++;
  4114. if (index < size) {
  4115. value = collection[keys[index]];
  4116. iterator(value, createCallback(value));
  4117. }
  4118. }
  4119. function objectIteratorWithKey() {
  4120. if (started < size) {
  4121. key = keys[started++];
  4122. value = collection[key];
  4123. iterator(value, key, createCallback(value));
  4124. }
  4125. }
  4126. function createCallback(value) {
  4127. var called = false;
  4128. return function(err, res) {
  4129. if (called) {
  4130. throwError();
  4131. }
  4132. called = true;
  4133. if (err) {
  4134. iterate = noop;
  4135. callback = once(callback);
  4136. callback(err);
  4137. } else if (!!res === bool) {
  4138. iterate = noop;
  4139. callback = once(callback);
  4140. callback(null, value);
  4141. } else if (++completed === size) {
  4142. callback(null);
  4143. } else if (sync) {
  4144. nextTick(iterate);
  4145. } else {
  4146. sync = true;
  4147. iterate();
  4148. }
  4149. sync = false;
  4150. };
  4151. }
  4152. };
  4153. }
  4154. /**
  4155. * @private
  4156. * @param {Function} arrayEach
  4157. * @param {Function} baseEach
  4158. * @param {Function} symbolEach
  4159. * @param {boolean} bool
  4160. */
  4161. function createPick(arrayEach, baseEach, symbolEach, bool) {
  4162. return function(collection, iterator, callback) {
  4163. callback = callback || noop;
  4164. var size, keys;
  4165. var completed = 0;
  4166. var result = {};
  4167. if (isArray(collection)) {
  4168. size = collection.length;
  4169. arrayEach(collection, iterator, createCallback);
  4170. } else if (!collection) {
  4171. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4172. size = collection.size;
  4173. symbolEach(collection, iterator, createCallback);
  4174. } else if (typeof collection === obj) {
  4175. keys = nativeKeys(collection);
  4176. size = keys.length;
  4177. baseEach(collection, iterator, createCallback, keys);
  4178. }
  4179. if (!size) {
  4180. return callback(null, {});
  4181. }
  4182. function createCallback(key, value) {
  4183. return function done(err, res) {
  4184. if (key === null) {
  4185. throwError();
  4186. }
  4187. if (err) {
  4188. key = null;
  4189. callback = once(callback);
  4190. callback(err, objectClone(result));
  4191. return;
  4192. }
  4193. if (!!res === bool) {
  4194. result[key] = value;
  4195. }
  4196. key = null;
  4197. if (++completed === size) {
  4198. callback(null, result);
  4199. }
  4200. };
  4201. }
  4202. };
  4203. }
  4204. /**
  4205. * @private
  4206. * @param {boolean} bool
  4207. */
  4208. function createPickSeries(bool) {
  4209. return function(collection, iterator, callback) {
  4210. callback = onlyOnce(callback || noop);
  4211. var size, key, value, keys, iter, iterate;
  4212. var sync = false;
  4213. var result = {};
  4214. var completed = 0;
  4215. if (isArray(collection)) {
  4216. size = collection.length;
  4217. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  4218. } else if (!collection) {
  4219. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4220. size = collection.size;
  4221. iter = collection[iteratorSymbol]();
  4222. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  4223. } else if (typeof collection === obj) {
  4224. keys = nativeKeys(collection);
  4225. size = keys.length;
  4226. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  4227. }
  4228. if (!size) {
  4229. return callback(null, {});
  4230. }
  4231. iterate();
  4232. function arrayIterator() {
  4233. key = completed;
  4234. value = collection[completed];
  4235. iterator(value, done);
  4236. }
  4237. function arrayIteratorWithIndex() {
  4238. key = completed;
  4239. value = collection[completed];
  4240. iterator(value, completed, done);
  4241. }
  4242. function symbolIterator() {
  4243. key = completed;
  4244. value = iter.next().value;
  4245. iterator(value, done);
  4246. }
  4247. function symbolIteratorWithKey() {
  4248. key = completed;
  4249. value = iter.next().value;
  4250. iterator(value, key, done);
  4251. }
  4252. function objectIterator() {
  4253. key = keys[completed];
  4254. value = collection[key];
  4255. iterator(value, done);
  4256. }
  4257. function objectIteratorWithKey() {
  4258. key = keys[completed];
  4259. value = collection[key];
  4260. iterator(value, key, done);
  4261. }
  4262. function done(err, res) {
  4263. if (err) {
  4264. callback(err, result);
  4265. return;
  4266. }
  4267. if (!!res === bool) {
  4268. result[key] = value;
  4269. }
  4270. if (++completed === size) {
  4271. iterate = throwError;
  4272. callback(null, result);
  4273. } else if (sync) {
  4274. nextTick(iterate);
  4275. } else {
  4276. sync = true;
  4277. iterate();
  4278. }
  4279. sync = false;
  4280. }
  4281. };
  4282. }
  4283. /**
  4284. * @private
  4285. * @param {boolean} bool
  4286. */
  4287. function createPickLimit(bool) {
  4288. return function(collection, limit, iterator, callback) {
  4289. callback = callback || noop;
  4290. var size, index, key, value, keys, iter, item, iterate;
  4291. var sync = false;
  4292. var result = {};
  4293. var started = 0;
  4294. var completed = 0;
  4295. if (isArray(collection)) {
  4296. size = collection.length;
  4297. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  4298. } else if (!collection) {
  4299. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4300. size = collection.size;
  4301. iter = collection[iteratorSymbol]();
  4302. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  4303. } else if (typeof collection === obj) {
  4304. keys = nativeKeys(collection);
  4305. size = keys.length;
  4306. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  4307. }
  4308. if (!size || isNaN(limit) || limit < 1) {
  4309. return callback(null, {});
  4310. }
  4311. timesSync(limit > size ? size : limit, iterate);
  4312. function arrayIterator() {
  4313. index = started++;
  4314. if (index < size) {
  4315. value = collection[index];
  4316. iterator(value, createCallback(value, index));
  4317. }
  4318. }
  4319. function arrayIteratorWithIndex() {
  4320. index = started++;
  4321. if (index < size) {
  4322. value = collection[index];
  4323. iterator(value, index, createCallback(value, index));
  4324. }
  4325. }
  4326. function symbolIterator() {
  4327. if ((item = iter.next()).done === false) {
  4328. value = item.value;
  4329. iterator(value, createCallback(value, started++));
  4330. }
  4331. }
  4332. function symbolIteratorWithKey() {
  4333. if ((item = iter.next()).done === false) {
  4334. value = item.value;
  4335. iterator(value, started, createCallback(value, started++));
  4336. }
  4337. }
  4338. function objectIterator() {
  4339. if (started < size) {
  4340. key = keys[started++];
  4341. value = collection[key];
  4342. iterator(value, createCallback(value, key));
  4343. }
  4344. }
  4345. function objectIteratorWithKey() {
  4346. if (started < size) {
  4347. key = keys[started++];
  4348. value = collection[key];
  4349. iterator(value, key, createCallback(value, key));
  4350. }
  4351. }
  4352. function createCallback(value, key) {
  4353. return function(err, res) {
  4354. if (key === null) {
  4355. throwError();
  4356. }
  4357. if (err) {
  4358. key = null;
  4359. iterate = noop;
  4360. callback = once(callback);
  4361. callback(err, objectClone(result));
  4362. return;
  4363. }
  4364. if (!!res === bool) {
  4365. result[key] = value;
  4366. }
  4367. key = null;
  4368. if (++completed === size) {
  4369. iterate = throwError;
  4370. callback = onlyOnce(callback);
  4371. callback(null, result);
  4372. } else if (sync) {
  4373. nextTick(iterate);
  4374. } else {
  4375. sync = true;
  4376. iterate();
  4377. }
  4378. sync = false;
  4379. };
  4380. }
  4381. };
  4382. }
  4383. /**
  4384. * @memberof async
  4385. * @namespace reduce
  4386. * @param {Array|Object} collection
  4387. * @param {*} result
  4388. * @param {Function} iterator
  4389. * @param {Function} callback
  4390. * @example
  4391. *
  4392. * // array
  4393. * var order = [];
  4394. * var collection = [1, 3, 2, 4];
  4395. * var iterator = function(result, num, done) {
  4396. * setTimeout(function() {
  4397. * order.push(num);
  4398. * done(null, result + num);
  4399. * }, num * 10);
  4400. * };
  4401. * async.reduce(collection, 0, iterator, function(err, res) {
  4402. * console.log(res); // 10
  4403. * console.log(order); // [1, 3, 2, 4]
  4404. * });
  4405. *
  4406. * @example
  4407. *
  4408. * // array with index
  4409. * var order = [];
  4410. * var collection = [1, 3, 2, 4];
  4411. * var iterator = function(result, num, index, done) {
  4412. * setTimeout(function() {
  4413. * order.push([num, index]);
  4414. * done(null, result + num);
  4415. * }, num * 10);
  4416. * };
  4417. * async.reduce(collection, '', iterator, function(err, res) {
  4418. * console.log(res); // '1324'
  4419. * console.log(order); // [[1, 0], [3, 1], [2, 2], [4, 3]]
  4420. * });
  4421. *
  4422. * @example
  4423. *
  4424. * // object
  4425. * var order = [];
  4426. * var object = { a: 1, b: 3, c: 2, d: 4 };
  4427. * var iterator = function(result, num, done) {
  4428. * setTimeout(function() {
  4429. * order.push(num);
  4430. * done(null, result + num);
  4431. * }, num * 10);
  4432. * };
  4433. * async.reduce(collection, '', iterator, function(err, res) {
  4434. * console.log(res); // '1324'
  4435. * console.log(order); // [1, 3, 2, 4]
  4436. * });
  4437. *
  4438. * @example
  4439. *
  4440. * // object with key
  4441. * var order = [];
  4442. * var object = { a: 1, b: 3, c: 2, d: 4 };
  4443. * var iterator = function(result, num, key, done) {
  4444. * setTimeout(function() {
  4445. * order.push([num, key]);
  4446. * done(null, result + num);
  4447. * }, num * 10);
  4448. * };
  4449. * async.reduce(collection, 0, iterator, function(err, res) {
  4450. * console.log(res); // 10
  4451. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b'], [4, 'd']]
  4452. * });
  4453. *
  4454. */
  4455. function reduce(collection, result, iterator, callback) {
  4456. callback = onlyOnce(callback || noop);
  4457. var size, key, keys, iter, iterate;
  4458. var sync = false;
  4459. var completed = 0;
  4460. if (isArray(collection)) {
  4461. size = collection.length;
  4462. iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
  4463. } else if (!collection) {
  4464. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4465. size = collection.size;
  4466. iter = collection[iteratorSymbol]();
  4467. iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;
  4468. } else if (typeof collection === obj) {
  4469. keys = nativeKeys(collection);
  4470. size = keys.length;
  4471. iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
  4472. }
  4473. if (!size) {
  4474. return callback(null, result);
  4475. }
  4476. iterate(result);
  4477. function arrayIterator(result) {
  4478. iterator(result, collection[completed], done);
  4479. }
  4480. function arrayIteratorWithIndex(result) {
  4481. iterator(result, collection[completed], completed, done);
  4482. }
  4483. function symbolIterator() {
  4484. iterator(result, iter.next().value, done);
  4485. }
  4486. function symbolIteratorWithKey() {
  4487. iterator(result, iter.next().value, completed, done);
  4488. }
  4489. function objectIterator(result) {
  4490. iterator(result, collection[keys[completed]], done);
  4491. }
  4492. function objectIteratorWithKey(result) {
  4493. key = keys[completed];
  4494. iterator(result, collection[key], key, done);
  4495. }
  4496. function done(err, result) {
  4497. if (err) {
  4498. callback(err, result);
  4499. } else if (++completed === size) {
  4500. iterator = throwError;
  4501. callback(null, result);
  4502. } else if (sync) {
  4503. nextTick(function() {
  4504. iterate(result);
  4505. });
  4506. } else {
  4507. sync = true;
  4508. iterate(result);
  4509. }
  4510. sync = false;
  4511. }
  4512. }
  4513. /**
  4514. * @memberof async
  4515. * @namespace reduceRight
  4516. * @param {Array|Object} collection
  4517. * @param {*} result
  4518. * @param {Function} iterator
  4519. * @param {Function} callback
  4520. * @example
  4521. *
  4522. * // array
  4523. * var order = [];
  4524. * var collection = [1, 3, 2, 4];
  4525. * var iterator = function(result, num, done) {
  4526. * setTimeout(function() {
  4527. * order.push(num);
  4528. * done(null, result + num);
  4529. * }, num * 10);
  4530. * };
  4531. * async.reduceRight(collection, 0, iterator, function(err, res) {
  4532. * console.log(res); // 10
  4533. * console.log(order); // [4, 2, 3, 1]
  4534. * });
  4535. *
  4536. * @example
  4537. *
  4538. * // array with index
  4539. * var order = [];
  4540. * var collection = [1, 3, 2, 4];
  4541. * var iterator = function(result, num, index, done) {
  4542. * setTimeout(function() {
  4543. * order.push([num, index]);
  4544. * done(null, result + num);
  4545. * }, num * 10);
  4546. * };
  4547. * async.reduceRight(collection, '', iterator, function(err, res) {
  4548. * console.log(res); // '4231'
  4549. * console.log(order); // [[4, 3], [2, 2], [3, 1], [1, 0]]
  4550. * });
  4551. *
  4552. * @example
  4553. *
  4554. * // object
  4555. * var order = [];
  4556. * var object = { a: 1, b: 3, c: 2, d: 4 };
  4557. * var iterator = function(result, num, done) {
  4558. * setTimeout(function() {
  4559. * order.push(num);
  4560. * done(null, result + num);
  4561. * }, num * 10);
  4562. * };
  4563. * async.reduceRight(collection, '', iterator, function(err, res) {
  4564. * console.log(res); // '4231'
  4565. * console.log(order); // [4, 2, 3, 1]
  4566. * });
  4567. *
  4568. * @example
  4569. *
  4570. * // object with key
  4571. * var order = [];
  4572. * var object = { a: 1, b: 3, c: 2, d: 4 };
  4573. * var iterator = function(result, num, key, done) {
  4574. * setTimeout(function() {
  4575. * order.push([num, key]);
  4576. * done(null, result + num);
  4577. * }, num * 10);
  4578. * };
  4579. * async.reduceRight(collection, 0, iterator, function(err, res) {
  4580. * console.log(res); // 10
  4581. * console.log(order); // [[4, 3], [2, 2], [3, 1], [1, 0]]
  4582. * });
  4583. *
  4584. */
  4585. function reduceRight(collection, result, iterator, callback) {
  4586. callback = onlyOnce(callback || noop);
  4587. var resIndex, index, key, keys, iter, item, col, iterate;
  4588. var sync = false;
  4589. if (isArray(collection)) {
  4590. resIndex = collection.length;
  4591. iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
  4592. } else if (!collection) {
  4593. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4594. resIndex = collection.size;
  4595. col = Array(resIndex);
  4596. iter = collection[iteratorSymbol]();
  4597. index = -1;
  4598. while ((item = iter.next()).done === false) {
  4599. col[++index] = item.value;
  4600. }
  4601. collection = col;
  4602. iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
  4603. } else if (typeof collection === obj) {
  4604. keys = nativeKeys(collection);
  4605. resIndex = keys.length;
  4606. iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
  4607. }
  4608. if (!resIndex) {
  4609. return callback(null, result);
  4610. }
  4611. iterate(result);
  4612. function arrayIterator(result) {
  4613. iterator(result, collection[--resIndex], done);
  4614. }
  4615. function arrayIteratorWithIndex(result) {
  4616. iterator(result, collection[--resIndex], resIndex, done);
  4617. }
  4618. function objectIterator(result) {
  4619. iterator(result, collection[keys[--resIndex]], done);
  4620. }
  4621. function objectIteratorWithKey(result) {
  4622. key = keys[--resIndex];
  4623. iterator(result, collection[key], key, done);
  4624. }
  4625. function done(err, result) {
  4626. if (err) {
  4627. callback(err, result);
  4628. } else if (resIndex === 0) {
  4629. iterate = throwError;
  4630. callback(null, result);
  4631. } else if (sync) {
  4632. nextTick(function() {
  4633. iterate(result);
  4634. });
  4635. } else {
  4636. sync = true;
  4637. iterate(result);
  4638. }
  4639. sync = false;
  4640. }
  4641. }
  4642. /**
  4643. * @private
  4644. * @param {Function} arrayEach
  4645. * @param {Function} baseEach
  4646. * @param {Function} symbolEach
  4647. */
  4648. function createTransform(arrayEach, baseEach, symbolEach) {
  4649. return function transform(collection, accumulator, iterator, callback) {
  4650. if (arguments.length === 3) {
  4651. callback = iterator;
  4652. iterator = accumulator;
  4653. accumulator = undefined;
  4654. }
  4655. callback = callback || noop;
  4656. var size, keys, result;
  4657. var completed = 0;
  4658. if (isArray(collection)) {
  4659. size = collection.length;
  4660. result = accumulator !== undefined ? accumulator : [];
  4661. arrayEach(collection, result, iterator, done);
  4662. } else if (!collection) {
  4663. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4664. size = collection.size;
  4665. result = accumulator !== undefined ? accumulator : {};
  4666. symbolEach(collection, result, iterator, done);
  4667. } else if (typeof collection === obj) {
  4668. keys = nativeKeys(collection);
  4669. size = keys.length;
  4670. result = accumulator !== undefined ? accumulator : {};
  4671. baseEach(collection, result, iterator, done, keys);
  4672. }
  4673. if (!size) {
  4674. callback(null, accumulator !== undefined ? accumulator : result || {});
  4675. }
  4676. function done(err, bool) {
  4677. if (err) {
  4678. callback = once(callback);
  4679. callback(err, isArray(result) ? createArray(result) : objectClone(result));
  4680. } else if (++completed === size) {
  4681. callback(null, result);
  4682. } else if (bool === false) {
  4683. callback = once(callback);
  4684. callback(null, isArray(result) ? createArray(result) : objectClone(result));
  4685. }
  4686. }
  4687. };
  4688. }
  4689. /**
  4690. * @memberof async
  4691. * @namespace transformSeries
  4692. * @param {Array|Object} collection
  4693. * @param {Array|Object|Function} [accumulator]
  4694. * @param {Function} [iterator]
  4695. * @param {Function} [callback]
  4696. * @example
  4697. *
  4698. * // array
  4699. * var order = [];
  4700. * var collection = [1, 3, 2, 4];
  4701. * var iterator = function(result, num, done) {
  4702. * setTimeout(function() {
  4703. * order.push(num);
  4704. * result.push(num)
  4705. * done();
  4706. * }, num * 10);
  4707. * };
  4708. * async.transformSeries(collection, iterator, function(err, res) {
  4709. * console.log(res); // [1, 3, 2, 4]
  4710. * console.log(order); // [1, 3, 2, 4]
  4711. * });
  4712. *
  4713. * @example
  4714. *
  4715. * // array with index and accumulator
  4716. * var order = [];
  4717. * var collection = [1, 3, 2, 4];
  4718. * var iterator = function(result, num, index, done) {
  4719. * setTimeout(function() {
  4720. * order.push([num, index]);
  4721. * result[index] = num;
  4722. * done();
  4723. * }, num * 10);
  4724. * };
  4725. * async.transformSeries(collection, {}, iterator, function(err, res) {
  4726. * console.log(res); // { '0': 1, '1': 3, '2': 2, '3': 4 }
  4727. * console.log(order); // [[1, 0], [3, 1], [2, 2], [4, 3]]
  4728. * });
  4729. *
  4730. * @example
  4731. *
  4732. * // object with accumulator
  4733. * var order = [];
  4734. * var object = { a: 1, b: 3, c: 2, d: 4 };
  4735. * var iterator = function(result, num, done) {
  4736. * setTimeout(function() {
  4737. * order.push(num);
  4738. * result.push(num);
  4739. * done();
  4740. * }, num * 10);
  4741. * };
  4742. * async.transformSeries(collection, [], iterator, function(err, res) {
  4743. * console.log(res); // [1, 3, 2, 4]
  4744. * console.log(order); // [1, 3, 2, 4]
  4745. * });
  4746. *
  4747. * @example
  4748. *
  4749. * // object with key
  4750. * var order = [];
  4751. * var object = { a: 1, b: 3, c: 2, d: 4 };
  4752. * var iterator = function(result, num, key, done) {
  4753. * setTimeout(function() {
  4754. * order.push([num, key]);
  4755. * result[key] = num;
  4756. * done();
  4757. * }, num * 10);
  4758. * };
  4759. * async.transformSeries(collection, iterator, function(err, res) {
  4760. * console.log(res); // { a: 1, b: 3, c: 2, d: 4 }
  4761. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'b'], [4, 'd']]
  4762. * });
  4763. *
  4764. */
  4765. function transformSeries(collection, accumulator, iterator, callback) {
  4766. if (arguments.length === 3) {
  4767. callback = iterator;
  4768. iterator = accumulator;
  4769. accumulator = undefined;
  4770. }
  4771. callback = onlyOnce(callback || noop);
  4772. var size, key, keys, iter, iterate, result;
  4773. var sync = false;
  4774. var completed = 0;
  4775. if (isArray(collection)) {
  4776. size = collection.length;
  4777. result = accumulator !== undefined ? accumulator : [];
  4778. iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
  4779. } else if (!collection) {
  4780. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4781. size = collection.size;
  4782. iter = collection[iteratorSymbol]();
  4783. result = accumulator !== undefined ? accumulator : {};
  4784. iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;
  4785. } else if (typeof collection === obj) {
  4786. keys = nativeKeys(collection);
  4787. size = keys.length;
  4788. result = accumulator !== undefined ? accumulator : {};
  4789. iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
  4790. }
  4791. if (!size) {
  4792. return callback(null, accumulator !== undefined ? accumulator : result || {});
  4793. }
  4794. iterate();
  4795. function arrayIterator() {
  4796. iterator(result, collection[completed], done);
  4797. }
  4798. function arrayIteratorWithIndex() {
  4799. iterator(result, collection[completed], completed, done);
  4800. }
  4801. function symbolIterator() {
  4802. iterator(result, iter.next().value, done);
  4803. }
  4804. function symbolIteratorWithKey() {
  4805. iterator(result, iter.next().value, completed, done);
  4806. }
  4807. function objectIterator() {
  4808. iterator(result, collection[keys[completed]], done);
  4809. }
  4810. function objectIteratorWithKey() {
  4811. key = keys[completed];
  4812. iterator(result, collection[key], key, done);
  4813. }
  4814. function done(err, bool) {
  4815. if (err) {
  4816. callback(err, result);
  4817. } else if (++completed === size) {
  4818. iterate = throwError;
  4819. callback(null, result);
  4820. } else if (bool === false) {
  4821. iterate = throwError;
  4822. callback(null, result);
  4823. } else if (sync) {
  4824. nextTick(iterate);
  4825. } else {
  4826. sync = true;
  4827. iterate();
  4828. }
  4829. sync = false;
  4830. }
  4831. }
  4832. /**
  4833. * @memberof async
  4834. * @namespace transformLimit
  4835. * @param {Array|Object} collection
  4836. * @param {number} limit - limit >= 1
  4837. * @param {Array|Object|Function} [accumulator]
  4838. * @param {Function} [iterator]
  4839. * @param {Function} [callback]
  4840. * @example
  4841. *
  4842. * // array
  4843. * var order = [];
  4844. * var array = [1, 5, 3, 4, 2];
  4845. * var iterator = function(result, num, done) {
  4846. * setTimeout(function() {
  4847. * order.push(num);
  4848. * result.push(num);
  4849. * done();
  4850. * }, num * 10);
  4851. * };
  4852. * async.transformLimit(array, 2, iterator, function(err, res) {
  4853. * console.log(res); // [1, 3, 5, 2, 4]
  4854. * console.log(order); // [1, 3, 5, 2, 4]
  4855. * });
  4856. *
  4857. * @example
  4858. *
  4859. * // array with index and accumulator
  4860. * var order = [];
  4861. * var array = [1, 5, 3, 4, 2];
  4862. * var iterator = function(result, num, index, done) {
  4863. * setTimeout(function() {
  4864. * order.push([num, index]);
  4865. * result[index] = key;
  4866. * done();
  4867. * }, num * 10);
  4868. * };
  4869. * async.transformLimit(array, 2, {}, iterator, function(err, res) {
  4870. * console.log(res); // { '0': 1, '1': 5, '2': 3, '3': 4, '4': 2 }
  4871. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  4872. * });
  4873. *
  4874. * @example
  4875. *
  4876. * // object with accumulator
  4877. * var order = [];
  4878. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  4879. * var iterator = function(result, num, done) {
  4880. * setTimeout(function() {
  4881. * order.push(num);
  4882. * result.push(num);
  4883. * done();
  4884. * }, num * 10);
  4885. * };
  4886. * async.transformLimit(object, 2, [], iterator, function(err, res) {
  4887. * console.log(res); // [1, 3, 5, 2, 4]
  4888. * console.log(order); // [1, 3, 5, 2, 4]
  4889. * });
  4890. *
  4891. * @example
  4892. *
  4893. * // object with key
  4894. * var order = [];
  4895. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  4896. * var iterator = function(result, num, key, done) {
  4897. * setTimeout(function() {
  4898. * order.push([num, key]);
  4899. * result[key] = num;
  4900. * done();
  4901. * }, num * 10);
  4902. * };
  4903. * async.transformLimit(object, 2, iterator, function(err, res) {
  4904. * console.log(res); // { a: 1, b: 5, c: 3, d: 4, e: 2 };
  4905. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  4906. * });
  4907. *
  4908. */
  4909. function transformLimit(collection, limit, accumulator, iterator, callback) {
  4910. if (arguments.length === 4) {
  4911. callback = iterator;
  4912. iterator = accumulator;
  4913. accumulator = undefined;
  4914. }
  4915. callback = callback || noop;
  4916. var size, index, key, keys, iter, item, iterate, result;
  4917. var sync = false;
  4918. var started = 0;
  4919. var completed = 0;
  4920. if (isArray(collection)) {
  4921. size = collection.length;
  4922. result = accumulator !== undefined ? accumulator : [];
  4923. iterate = iterator.length === 4 ? arrayIteratorWithIndex : arrayIterator;
  4924. } else if (!collection) {
  4925. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  4926. size = collection.size;
  4927. iter = collection[iteratorSymbol]();
  4928. result = accumulator !== undefined ? accumulator : {};
  4929. iterate = iterator.length === 4 ? symbolIteratorWithKey : symbolIterator;
  4930. } else if (typeof collection === obj) {
  4931. keys = nativeKeys(collection);
  4932. size = keys.length;
  4933. result = accumulator !== undefined ? accumulator : {};
  4934. iterate = iterator.length === 4 ? objectIteratorWithKey : objectIterator;
  4935. }
  4936. if (!size || isNaN(limit) || limit < 1) {
  4937. return callback(null, accumulator !== undefined ? accumulator : result || {});
  4938. }
  4939. timesSync(limit > size ? size : limit, iterate);
  4940. function arrayIterator() {
  4941. index = started++;
  4942. if (index < size) {
  4943. iterator(result, collection[index], onlyOnce(done));
  4944. }
  4945. }
  4946. function arrayIteratorWithIndex() {
  4947. index = started++;
  4948. if (index < size) {
  4949. iterator(result, collection[index], index, onlyOnce(done));
  4950. }
  4951. }
  4952. function symbolIterator() {
  4953. if ((item = iter.next()).done === false) {
  4954. iterator(result, item.value, onlyOnce(done));
  4955. }
  4956. }
  4957. function symbolIteratorWithKey() {
  4958. if ((item = iter.next()).done === false) {
  4959. iterator(result, item.value, started++, onlyOnce(done));
  4960. }
  4961. }
  4962. function objectIterator() {
  4963. index = started++;
  4964. if (index < size) {
  4965. iterator(result, collection[keys[index]], onlyOnce(done));
  4966. }
  4967. }
  4968. function objectIteratorWithKey() {
  4969. index = started++;
  4970. if (index < size) {
  4971. key = keys[index];
  4972. iterator(result, collection[key], key, onlyOnce(done));
  4973. }
  4974. }
  4975. function done(err, bool) {
  4976. if (err) {
  4977. iterate = noop;
  4978. callback(err, isArray(result) ? createArray(result) : objectClone(result));
  4979. callback = noop;
  4980. } else if (++completed === size) {
  4981. callback(null, result);
  4982. } else if (bool === false) {
  4983. iterate = noop;
  4984. callback(null, isArray(result) ? createArray(result) : objectClone(result));
  4985. callback = noop;
  4986. } else if (sync) {
  4987. nextTick(iterate);
  4988. } else {
  4989. sync = true;
  4990. iterate();
  4991. }
  4992. sync = false;
  4993. }
  4994. }
  4995. /**
  4996. * @private
  4997. * @param {function} arrayEach
  4998. * @param {function} baseEach
  4999. * @param {function} symbolEach
  5000. */
  5001. function createSortBy(arrayEach, baseEach, symbolEach) {
  5002. return function sortBy(collection, iterator, callback) {
  5003. callback = callback || noop;
  5004. var size, array, criteria;
  5005. var completed = 0;
  5006. if (isArray(collection)) {
  5007. size = collection.length;
  5008. array = Array(size);
  5009. criteria = Array(size);
  5010. arrayEach(collection, iterator, createCallback);
  5011. } else if (!collection) {
  5012. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  5013. size = collection.size;
  5014. array = Array(size);
  5015. criteria = Array(size);
  5016. symbolEach(collection, iterator, createCallback);
  5017. } else if (typeof collection === obj) {
  5018. var keys = nativeKeys(collection);
  5019. size = keys.length;
  5020. array = Array(size);
  5021. criteria = Array(size);
  5022. baseEach(collection, iterator, createCallback, keys);
  5023. }
  5024. if (!size) {
  5025. callback(null, []);
  5026. }
  5027. function createCallback(index, value) {
  5028. var called = false;
  5029. array[index] = value;
  5030. return function done(err, criterion) {
  5031. if (called) {
  5032. throwError();
  5033. }
  5034. called = true;
  5035. criteria[index] = criterion;
  5036. if (err) {
  5037. callback = once(callback);
  5038. callback(err);
  5039. } else if (++completed === size) {
  5040. callback(null, sortByCriteria(array, criteria));
  5041. }
  5042. };
  5043. }
  5044. };
  5045. }
  5046. /**
  5047. * @memberof async
  5048. * @namespace sortBySeries
  5049. * @param {Array|Object} collection
  5050. * @param {Function} iterator
  5051. * @param {Function} callback
  5052. * @example
  5053. *
  5054. * // array
  5055. * var order = [];
  5056. * var array = [1, 3, 2];
  5057. * var iterator = function(num, done) {
  5058. * setTimeout(function() {
  5059. * order.push(num);
  5060. * done(null, num);
  5061. * }, num * 10);
  5062. * };
  5063. * async.sortBySeries(array, iterator, function(err, res) {
  5064. * console.log(res); // [1, 2, 3];
  5065. * console.log(order); // [1, 3, 2]
  5066. * });
  5067. *
  5068. * @example
  5069. *
  5070. * // array with index
  5071. * var order = [];
  5072. * var array = [1, 3, 2];
  5073. * var iterator = function(num, index, done) {
  5074. * setTimeout(function() {
  5075. * order.push([num, index]);
  5076. * done(null, num);
  5077. * }, num * 10);
  5078. * };
  5079. * async.sortBySeries(array, iterator, function(err, res) {
  5080. * console.log(res); // [1, 2, 3]
  5081. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  5082. * });
  5083. *
  5084. * @example
  5085. *
  5086. * // object
  5087. * var order = [];
  5088. * var object = { a: 1, b: 3, c: 2 };
  5089. * var iterator = function(num, done) {
  5090. * setTimeout(function() {
  5091. * order.push(num);
  5092. * done(null, num);
  5093. * }, num * 10);
  5094. * };
  5095. * async.sortBySeries(object, iterator, function(err, res) {
  5096. * console.log(res); // [1, 2, 3]
  5097. * console.log(order); // [1, 3, 2]
  5098. * });
  5099. *
  5100. * @example
  5101. *
  5102. * // object with key
  5103. * var order = [];
  5104. * var object = { a: 1, b: 3, c: 2 };
  5105. * var iterator = function(num, key, done) {
  5106. * setTimeout(function() {
  5107. * order.push([num, key]);
  5108. * done(null, num);
  5109. * }, num * 10);
  5110. * };
  5111. * async.sortBySeries(object, iterator, function(err, res) {
  5112. * console.log(res); // [1, 2, 3]
  5113. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
  5114. * });
  5115. *
  5116. */
  5117. function sortBySeries(collection, iterator, callback) {
  5118. callback = onlyOnce(callback || noop);
  5119. var size, key, value, keys, iter, array, criteria, iterate;
  5120. var sync = false;
  5121. var completed = 0;
  5122. if (isArray(collection)) {
  5123. size = collection.length;
  5124. array = collection;
  5125. criteria = Array(size);
  5126. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  5127. } else if (!collection) {
  5128. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  5129. size = collection.size;
  5130. array = Array(size);
  5131. criteria = Array(size);
  5132. iter = collection[iteratorSymbol]();
  5133. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  5134. } else if (typeof collection === obj) {
  5135. keys = nativeKeys(collection);
  5136. size = keys.length;
  5137. array = Array(size);
  5138. criteria = Array(size);
  5139. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  5140. }
  5141. if (!size) {
  5142. return callback(null, []);
  5143. }
  5144. iterate();
  5145. function arrayIterator() {
  5146. value = collection[completed];
  5147. iterator(value, done);
  5148. }
  5149. function arrayIteratorWithIndex() {
  5150. value = collection[completed];
  5151. iterator(value, completed, done);
  5152. }
  5153. function symbolIterator() {
  5154. value = iter.next().value;
  5155. array[completed] = value;
  5156. iterator(value, done);
  5157. }
  5158. function symbolIteratorWithKey() {
  5159. value = iter.next().value;
  5160. array[completed] = value;
  5161. iterator(value, completed, done);
  5162. }
  5163. function objectIterator() {
  5164. value = collection[keys[completed]];
  5165. array[completed] = value;
  5166. iterator(value, done);
  5167. }
  5168. function objectIteratorWithKey() {
  5169. key = keys[completed];
  5170. value = collection[key];
  5171. array[completed] = value;
  5172. iterator(value, key, done);
  5173. }
  5174. function done(err, criterion) {
  5175. criteria[completed] = criterion;
  5176. if (err) {
  5177. callback(err);
  5178. } else if (++completed === size) {
  5179. iterate = throwError;
  5180. callback(null, sortByCriteria(array, criteria));
  5181. } else if (sync) {
  5182. nextTick(iterate);
  5183. } else {
  5184. sync = true;
  5185. iterate();
  5186. }
  5187. sync = false;
  5188. }
  5189. }
  5190. /**
  5191. * @memberof async
  5192. * @namespace sortByLimit
  5193. * @param {Array|Object} collection
  5194. * @param {number} limit - limit >= 1
  5195. * @param {Function} iterator
  5196. * @param {Function} callback
  5197. * @example
  5198. *
  5199. * // array
  5200. * var order = [];
  5201. * var array = [1, 5, 3, 4, 2];
  5202. * var iterator = function(num, done) {
  5203. * setTimeout(function() {
  5204. * order.push(num);
  5205. * done(null, num);
  5206. * }, num * 10);
  5207. * };
  5208. * async.sortByLimit(array, 2, iterator, function(err, res) {
  5209. * console.log(res); // [1, 2, 3, 4, 5]
  5210. * console.log(order); // [1, 3, 5, 2, 4]
  5211. * });
  5212. *
  5213. * @example
  5214. *
  5215. * // array with index
  5216. * var order = [];
  5217. * var array = [1, 5, 3, 4, 2];
  5218. * var iterator = function(num, index, done) {
  5219. * setTimeout(function() {
  5220. * order.push([num, index]);
  5221. * done(null, num);
  5222. * }, num * 10);
  5223. * };
  5224. * async.sortByLimit(array, 2, iterator, function(err, res) {
  5225. * console.log(res); // [1, 2, 3, 4, 5]
  5226. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  5227. * });
  5228. *
  5229. * @example
  5230. *
  5231. * // object
  5232. * var order = [];
  5233. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  5234. * var iterator = function(num, done) {
  5235. * setTimeout(function() {
  5236. * order.push(num);
  5237. * done(null, num);
  5238. * }, num * 10);
  5239. * };
  5240. * async.sortByLimit(object, 2, iterator, function(err, res) {
  5241. * console.log(res); // [1, 2, 3, 4, 5]
  5242. * console.log(order); // [1, 3, 5, 2, 4]
  5243. * });
  5244. *
  5245. * @example
  5246. *
  5247. * // object with key
  5248. * var order = [];
  5249. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  5250. * var iterator = function(num, key, done) {
  5251. * setTimeout(function() {
  5252. * order.push([num, key]);
  5253. * done(null, num);
  5254. * }, num * 10);
  5255. * };
  5256. * async.sortByLimit(object, 2, iterator, function(err, res) {
  5257. * console.log(res); // [1, 2, 3, 4, 5]
  5258. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  5259. * });
  5260. *
  5261. */
  5262. function sortByLimit(collection, limit, iterator, callback) {
  5263. callback = callback || noop;
  5264. var size, index, key, value, array, keys, iter, item, criteria, iterate;
  5265. var sync = false;
  5266. var started = 0;
  5267. var completed = 0;
  5268. if (isArray(collection)) {
  5269. size = collection.length;
  5270. array = collection;
  5271. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  5272. } else if (!collection) {
  5273. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  5274. size = collection.size;
  5275. iter = collection[iteratorSymbol]();
  5276. array = Array(size);
  5277. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  5278. } else if (typeof collection === obj) {
  5279. keys = nativeKeys(collection);
  5280. size = keys.length;
  5281. array = Array(size);
  5282. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  5283. }
  5284. if (!size || isNaN(limit) || limit < 1) {
  5285. return callback(null, []);
  5286. }
  5287. criteria = Array(size);
  5288. timesSync(limit > size ? size : limit, iterate);
  5289. function arrayIterator() {
  5290. if (started < size) {
  5291. value = collection[started];
  5292. iterator(value, createCallback(value, started++));
  5293. }
  5294. }
  5295. function arrayIteratorWithIndex() {
  5296. index = started++;
  5297. if (index < size) {
  5298. value = collection[index];
  5299. iterator(value, index, createCallback(value, index));
  5300. }
  5301. }
  5302. function symbolIterator() {
  5303. if ((item = iter.next()).done === false) {
  5304. value = item.value;
  5305. array[started] = value;
  5306. iterator(value, createCallback(value, started++));
  5307. }
  5308. }
  5309. function symbolIteratorWithKey() {
  5310. if ((item = iter.next()).done === false) {
  5311. value = item.value;
  5312. array[started] = value;
  5313. iterator(value, started, createCallback(value, started++));
  5314. }
  5315. }
  5316. function objectIterator() {
  5317. if (started < size) {
  5318. value = collection[keys[started]];
  5319. array[started] = value;
  5320. iterator(value, createCallback(value, started++));
  5321. }
  5322. }
  5323. function objectIteratorWithKey() {
  5324. if (started < size) {
  5325. key = keys[started];
  5326. value = collection[key];
  5327. array[started] = value;
  5328. iterator(value, key, createCallback(value, started++));
  5329. }
  5330. }
  5331. function createCallback(value, index) {
  5332. var called = false;
  5333. return function(err, criterion) {
  5334. if (called) {
  5335. throwError();
  5336. }
  5337. called = true;
  5338. criteria[index] = criterion;
  5339. if (err) {
  5340. iterate = noop;
  5341. callback(err);
  5342. callback = noop;
  5343. } else if (++completed === size) {
  5344. callback(null, sortByCriteria(array, criteria));
  5345. } else if (sync) {
  5346. nextTick(iterate);
  5347. } else {
  5348. sync = true;
  5349. iterate();
  5350. }
  5351. sync = false;
  5352. };
  5353. }
  5354. }
  5355. /**
  5356. * @memberof async
  5357. * @namespace some
  5358. * @param {Array|Object} collection
  5359. * @param {Function} iterator
  5360. * @param {Function} callback
  5361. * @example
  5362. *
  5363. * // array
  5364. * var order = [];
  5365. * var array = [1, 3, 2];
  5366. * var iterator = function(num, done) {
  5367. * setTimeout(function() {
  5368. * order.push(num);
  5369. * done(null, num % 2);
  5370. * }, num * 10);
  5371. * };
  5372. * async.some(array, iterator, function(err, res) {
  5373. * console.log(res); // true
  5374. * console.log(order); // [1]
  5375. * });
  5376. *
  5377. * @example
  5378. *
  5379. * // array with index
  5380. * var order = [];
  5381. * var array = [1, 3, 2];
  5382. * var iterator = function(num, index, done) {
  5383. * setTimeout(function() {
  5384. * order.push([num, index]);
  5385. * done(null, num % 2);
  5386. * }, num * 10);
  5387. * };
  5388. * async.some(array, iterator, function(err, res) {
  5389. * console.log(res); // true
  5390. * console.log(order); // [[1, 0]]
  5391. * });
  5392. *
  5393. * @example
  5394. *
  5395. * // object
  5396. * var order = [];
  5397. * var object = { a: 1, b: 3, c: 2 };
  5398. * var iterator = function(num, done) {
  5399. * setTimeout(function() {
  5400. * order.push(num);
  5401. * done(null, num % 2);
  5402. * }, num * 10);
  5403. * };
  5404. * async.some(object, iterator, function(err, res) {
  5405. * console.log(res); // true
  5406. * console.log(order); // [1]
  5407. * });
  5408. *
  5409. * @example
  5410. *
  5411. * // object with key
  5412. * var order = [];
  5413. * var object = { a: 1, b: 3, c: 2 };
  5414. * var iterator = function(num, key, done) {
  5415. * setTimeout(function() {
  5416. * order.push([num, key]);
  5417. * done(null, num % 2);
  5418. * }, num * 10);
  5419. * };
  5420. * async.some(object, iterator, function(err, res) {
  5421. * console.log(res); // true
  5422. * console.log(order); // [[1, 'a']]
  5423. * });
  5424. *
  5425. */
  5426. function some(collection, iterator, callback) {
  5427. callback = callback || noop;
  5428. detect(collection, iterator, done);
  5429. function done(err, res) {
  5430. if (err) {
  5431. return callback(err);
  5432. }
  5433. callback(null, !!res);
  5434. }
  5435. }
  5436. /**
  5437. * @memberof async
  5438. * @namespace someSeries
  5439. * @param {Array|Object} collection
  5440. * @param {Function} iterator
  5441. * @param {Function} callback
  5442. * @example
  5443. *
  5444. * // array
  5445. * var order = [];
  5446. * var array = [1, 3, 2];
  5447. * var iterator = function(num, done) {
  5448. * setTimeout(function() {
  5449. * order.push(num);
  5450. * done(null, num % 2);
  5451. * }, num * 10);
  5452. * };
  5453. * async.someSeries(array, iterator, function(err, res) {
  5454. * console.log(res); // true
  5455. * console.log(order); // [1]
  5456. * });
  5457. *
  5458. * @example
  5459. *
  5460. * // array with index
  5461. * var order = [];
  5462. * var array = [1, 3, 2];
  5463. * var iterator = function(num, index, done) {
  5464. * setTimeout(function() {
  5465. * order.push([num, index]);
  5466. * done(null, num % 2);
  5467. * }, num * 10);
  5468. * };
  5469. * async.someSeries(array, iterator, function(err, res) {
  5470. * console.log(res); // true
  5471. * console.log(order); // [[1, 0]]
  5472. * });
  5473. *
  5474. * @example
  5475. *
  5476. * // object
  5477. * var order = [];
  5478. * var object = { a: 1, b: 3, c: 2 };
  5479. * var iterator = function(num, done) {
  5480. * setTimeout(function() {
  5481. * order.push(num);
  5482. * done(null, num % 2);
  5483. * }, num * 10);
  5484. * };
  5485. * async.someSeries(object, iterator, function(err, res) {
  5486. * console.log(res); // true
  5487. * console.log(order); // [1]
  5488. * });
  5489. *
  5490. * @example
  5491. *
  5492. * // object with key
  5493. * var order = [];
  5494. * var object = { a: 1, b: 3, c: 2 };
  5495. * var iterator = function(num, key, done) {
  5496. * setTimeout(function() {
  5497. * order.push([num, key]);
  5498. * done(null, num % 2);
  5499. * }, num * 10);
  5500. * };
  5501. * async.someSeries(object, iterator, function(err, res) {
  5502. * console.log(res); // true
  5503. * console.log(order); // [[1, 'a']]
  5504. * });
  5505. *
  5506. */
  5507. function someSeries(collection, iterator, callback) {
  5508. callback = callback || noop;
  5509. detectSeries(collection, iterator, done);
  5510. function done(err, res) {
  5511. if (err) {
  5512. return callback(err);
  5513. }
  5514. callback(null, !!res);
  5515. }
  5516. }
  5517. /**
  5518. * @memberof async
  5519. * @namespace someLimit
  5520. * @param {Array|Object} collection
  5521. * @param {number} limit - limit >= 1
  5522. * @param {Function} iterator
  5523. * @param {Function} callback
  5524. * @example
  5525. *
  5526. * // array
  5527. * var order = [];
  5528. * var array = [1, 5, 3, 4, 2];
  5529. * var iterator = function(num, done) {
  5530. * setTimeout(function() {
  5531. * order.push(num);
  5532. * done(null, num % 2);
  5533. * }, num * 10);
  5534. * };
  5535. * async.someLimit(array, 2, iterator, function(err, res) {
  5536. * console.log(res); // true
  5537. * console.log(order); // [1]
  5538. * });
  5539. *
  5540. * @example
  5541. *
  5542. * // array with index
  5543. * var order = [];
  5544. * var array = [1, 5, 3, 4, 2];
  5545. * var iterator = function(num, index, done) {
  5546. * setTimeout(function() {
  5547. * order.push([num, index]);
  5548. * done(null, num % 2);
  5549. * }, num * 10);
  5550. * };
  5551. * async.someLimit(array, 2, iterator, function(err, res) {
  5552. * console.log(res); // true
  5553. * console.log(order); // [[1, 0]]
  5554. * });
  5555. *
  5556. * @example
  5557. *
  5558. * // object
  5559. * var order = [];
  5560. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  5561. * var iterator = function(num, done) {
  5562. * setTimeout(function() {
  5563. * order.push(num);
  5564. * done(null, num % 2);
  5565. * }, num * 10);
  5566. * };
  5567. * async.someLimit(object, 2, iterator, function(err, res) {
  5568. * console.log(res); // true
  5569. * console.log(order); // [1]
  5570. * });
  5571. *
  5572. * @example
  5573. *
  5574. * // object with key
  5575. * var order = [];
  5576. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  5577. * var iterator = function(num, key, done) {
  5578. * setTimeout(function() {
  5579. * order.push([num, key]);
  5580. * done(null, num % 2);
  5581. * }, num * 10);
  5582. * };
  5583. * async.someLimit(object, 2, iterator, function(err, res) {
  5584. * console.log(res); // true
  5585. * console.log(order); // [[1, 'a']]
  5586. * });
  5587. *
  5588. */
  5589. function someLimit(collection, limit, iterator, callback) {
  5590. callback = callback || noop;
  5591. detectLimit(collection, limit, iterator, done);
  5592. function done(err, res) {
  5593. if (err) {
  5594. return callback(err);
  5595. }
  5596. callback(null, !!res);
  5597. }
  5598. }
  5599. /**
  5600. * @private
  5601. * @param {Function} arrayEach
  5602. * @param {Function} baseEach
  5603. * @param {Function} symbolEach
  5604. */
  5605. function createEvery(arrayEach, baseEach, symbolEach) {
  5606. var deny = createDetect(arrayEach, baseEach, symbolEach, false);
  5607. return function every(collection, iterator, callback) {
  5608. callback = callback || noop;
  5609. deny(collection, iterator, done);
  5610. function done(err, res) {
  5611. if (err) {
  5612. return callback(err);
  5613. }
  5614. callback(null, !res);
  5615. }
  5616. };
  5617. }
  5618. /**
  5619. * @private
  5620. */
  5621. function createEverySeries() {
  5622. var denySeries = createDetectSeries(false);
  5623. return function everySeries(collection, iterator, callback) {
  5624. callback = callback || noop;
  5625. denySeries(collection, iterator, done);
  5626. function done(err, res) {
  5627. if (err) {
  5628. return callback(err);
  5629. }
  5630. callback(null, !res);
  5631. }
  5632. };
  5633. }
  5634. /**
  5635. * @private
  5636. */
  5637. function createEveryLimit() {
  5638. var denyLimit = createDetectLimit(false);
  5639. return function everyLimit(collection, limit, iterator, callback) {
  5640. callback = callback || noop;
  5641. denyLimit(collection, limit, iterator, done);
  5642. function done(err, res) {
  5643. if (err) {
  5644. return callback(err);
  5645. }
  5646. callback(null, !res);
  5647. }
  5648. };
  5649. }
  5650. /**
  5651. * @private
  5652. * @param {Function} arrayEach
  5653. * @param {Function} baseEach
  5654. * @param {Function} symbolEach
  5655. */
  5656. function createConcat(arrayEach, baseEach, symbolEach) {
  5657. return function concat(collection, iterator, callback) {
  5658. callback = callback || noop;
  5659. var size, result;
  5660. var completed = 0;
  5661. if (isArray(collection)) {
  5662. size = collection.length;
  5663. result = Array(size);
  5664. arrayEach(collection, iterator, createCallback);
  5665. } else if (!collection) {
  5666. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  5667. size = collection.size;
  5668. result = Array(size);
  5669. symbolEach(collection, iterator, createCallback);
  5670. } else if (typeof collection === obj) {
  5671. var keys = nativeKeys(collection);
  5672. size = keys.length;
  5673. result = Array(size);
  5674. baseEach(collection, iterator, createCallback, keys);
  5675. }
  5676. if (!size) {
  5677. callback(null, []);
  5678. }
  5679. function createCallback(index) {
  5680. return function done(err, res) {
  5681. if (index === null) {
  5682. throwError();
  5683. }
  5684. if (err) {
  5685. index = null;
  5686. callback = once(callback);
  5687. arrayEachSync(result, function(array, index) {
  5688. if (array === undefined) {
  5689. result[index] = noop;
  5690. }
  5691. });
  5692. callback(err, makeConcatResult(result));
  5693. return;
  5694. }
  5695. switch (arguments.length) {
  5696. case 0:
  5697. case 1:
  5698. result[index] = noop;
  5699. break;
  5700. case 2:
  5701. result[index] = res;
  5702. break;
  5703. default:
  5704. result[index] = slice(arguments, 1);
  5705. break;
  5706. }
  5707. index = null;
  5708. if (++completed === size) {
  5709. callback(null, makeConcatResult(result));
  5710. }
  5711. };
  5712. }
  5713. };
  5714. }
  5715. /**
  5716. * @memberof async
  5717. * @namespace concatSeries
  5718. * @param {Array|Object} collection
  5719. * @param {Function} iterator
  5720. * @param {Function} callback
  5721. * @example
  5722. *
  5723. * // array
  5724. * var order = [];
  5725. * var array = [1, 3, 2];
  5726. * var iterator = function(num, done) {
  5727. * setTimeout(function() {
  5728. * order.push(num);
  5729. * done(null, [num]);
  5730. * }, num * 10);
  5731. * };
  5732. * async.concatSeries(array, iterator, function(err, res) {
  5733. * console.log(res); // [1, 3, 2];
  5734. * console.log(order); // [1, 3, 2]
  5735. * });
  5736. *
  5737. * @example
  5738. *
  5739. * // array with index
  5740. * var order = [];
  5741. * var array = [1, 3, 2];
  5742. * var iterator = function(num, index, done) {
  5743. * setTimeout(function() {
  5744. * order.push([num, index]);
  5745. * done(null, [num]);
  5746. * }, num * 10);
  5747. * };
  5748. * async.concatSeries(array, iterator, function(err, res) {
  5749. * console.log(res); // [1, 3, 2]
  5750. * console.log(order); // [[1, 0], [3, 1], [2, 2]]
  5751. * });
  5752. *
  5753. * @example
  5754. *
  5755. * // object
  5756. * var order = [];
  5757. * var object = { a: 1, b: 3, c: 2 };
  5758. * var iterator = function(num, done) {
  5759. * setTimeout(function() {
  5760. * order.push(num);
  5761. * done(null, [num]);
  5762. * }, num * 10);
  5763. * };
  5764. * async.concatSeries(object, iterator, function(err, res) {
  5765. * console.log(res); // [1, 3, 2]
  5766. * console.log(order); // [1, 3, 2]
  5767. * });
  5768. *
  5769. * @example
  5770. *
  5771. * // object with key
  5772. * var order = [];
  5773. * var object = { a: 1, b: 3, c: 2 };
  5774. * var iterator = function(num, key, done) {
  5775. * setTimeout(function() {
  5776. * order.push([num, key]);
  5777. * done(null, [num]);
  5778. * }, num * 10);
  5779. * };
  5780. * async.concatSeries(object, iterator, function(err, res) {
  5781. * console.log(res); // [1, 3, 2]
  5782. * console.log(order); // [[1, 'a'], [3, 'b'], [2, 'c']]
  5783. * });
  5784. *
  5785. */
  5786. function concatSeries(collection, iterator, callback) {
  5787. callback = onlyOnce(callback || noop);
  5788. var size, key, keys, iter, values, iterate;
  5789. var sync = false;
  5790. var result = [];
  5791. var completed = 0;
  5792. if (isArray(collection)) {
  5793. size = collection.length;
  5794. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  5795. } else if (!collection) {
  5796. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  5797. size = collection.size;
  5798. iter = collection[iteratorSymbol]();
  5799. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  5800. } else if (typeof collection === obj) {
  5801. keys = nativeKeys(collection);
  5802. size = keys.length;
  5803. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  5804. }
  5805. if (!size) {
  5806. return callback(null, result);
  5807. }
  5808. iterate();
  5809. function arrayIterator() {
  5810. iterator(collection[completed], done);
  5811. }
  5812. function arrayIteratorWithIndex() {
  5813. iterator(collection[completed], completed, done);
  5814. }
  5815. function symbolIterator() {
  5816. iterator(iter.next().value, done);
  5817. }
  5818. function symbolIteratorWithKey() {
  5819. values = iter.next().value;
  5820. iterator(values, completed, done);
  5821. }
  5822. function objectIterator() {
  5823. iterator(collection[keys[completed]], done);
  5824. }
  5825. function objectIteratorWithKey() {
  5826. key = keys[completed];
  5827. iterator(collection[key], key, done);
  5828. }
  5829. function done(err, array) {
  5830. if (isArray(array)) {
  5831. nativePush.apply(result, array);
  5832. } else if (arguments.length >= 2) {
  5833. nativePush.apply(result, slice(arguments, 1));
  5834. }
  5835. if (err) {
  5836. callback(err, result);
  5837. } else if (++completed === size) {
  5838. iterate = throwError;
  5839. callback(null, result);
  5840. } else if (sync) {
  5841. nextTick(iterate);
  5842. } else {
  5843. sync = true;
  5844. iterate();
  5845. }
  5846. sync = false;
  5847. }
  5848. }
  5849. /**
  5850. * @memberof async
  5851. * @namespace concatLimit
  5852. * @param {Array|Object} collection
  5853. * @param {number} limit - limit >= 1
  5854. * @param {Function} iterator
  5855. * @param {Function} callback
  5856. * @example
  5857. *
  5858. * // array
  5859. * var order = [];
  5860. * var array = [1, 5, 3, 4, 2];
  5861. * var iterator = function(num, done) {
  5862. * setTimeout(function() {
  5863. * order.push(num);
  5864. * done(null, [num]);
  5865. * }, num * 10);
  5866. * };
  5867. * async.concatLimit(array, 2, iterator, function(err, res) {
  5868. * console.log(res); // [1, 3, 5, 2, 4]
  5869. * console.log(order); // [1, 3, 5, 2, 4]
  5870. * });
  5871. *
  5872. * @example
  5873. *
  5874. * // array with index
  5875. * var order = [];
  5876. * var array = [1, 5, 3, 4, 2];
  5877. * var iterator = function(num, index, done) {
  5878. * setTimeout(function() {
  5879. * order.push([num, index]);
  5880. * done(null, [num]);
  5881. * }, num * 10);
  5882. * };
  5883. * async.cocnatLimit(array, 2, iterator, function(err, res) {
  5884. * console.log(res); // [1, 3, 5, 2, 4]
  5885. * console.log(order); // [[1, 0], [3, 2], [5, 1], [2, 4], [4, 3]]
  5886. * });
  5887. *
  5888. * @example
  5889. *
  5890. * // object
  5891. * var order = [];
  5892. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  5893. * var iterator = function(num, done) {
  5894. * setTimeout(function() {
  5895. * order.push(num);
  5896. * done(null, [num]);
  5897. * }, num * 10);
  5898. * };
  5899. * async.concatLimit(object, 2, iterator, function(err, res) {
  5900. * console.log(res); // [1, 3, 5, 2, 4]
  5901. * console.log(order); // [1, 3, 5, 2, 4]
  5902. * });
  5903. *
  5904. * @example
  5905. *
  5906. * // object with key
  5907. * var order = [];
  5908. * var object = { a: 1, b: 5, c: 3, d: 4, e: 2 };
  5909. * var iterator = function(num, key, done) {
  5910. * setTimeout(function() {
  5911. * order.push([num, key]);
  5912. * done(null, num);
  5913. * }, num * 10);
  5914. * };
  5915. * async.cocnatLimit(object, 2, iterator, function(err, res) {
  5916. * console.log(res); // [1, 3, 5, 2, 4]
  5917. * console.log(order); // [[1, 'a'], [3, 'c'], [5, 'b'], [2, 'e'], [4, 'd']]
  5918. * });
  5919. *
  5920. */
  5921. function concatLimit(collection, limit, iterator, callback) {
  5922. callback = callback || noop;
  5923. var size, key, iter, item, iterate;
  5924. var sync = false;
  5925. var started = 0;
  5926. var completed = 0;
  5927. if (isArray(collection)) {
  5928. size = collection.length;
  5929. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  5930. } else if (!collection) {
  5931. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  5932. size = collection.size;
  5933. iter = collection[iteratorSymbol]();
  5934. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  5935. } else if (typeof collection === obj) {
  5936. var keys = nativeKeys(collection);
  5937. size = keys.length;
  5938. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  5939. }
  5940. if (!size || isNaN(limit) || limit < 1) {
  5941. return callback(null, []);
  5942. }
  5943. var result = Array(size);
  5944. timesSync(limit > size ? size : limit, iterate);
  5945. function arrayIterator() {
  5946. if (started < size) {
  5947. iterator(collection[started], createCallback(started++));
  5948. }
  5949. }
  5950. function arrayIteratorWithIndex() {
  5951. if (started < size) {
  5952. iterator(collection[started], started, createCallback(started++));
  5953. }
  5954. }
  5955. function symbolIterator() {
  5956. if ((item = iter.next()).done === false) {
  5957. iterator(item.value, createCallback(started++));
  5958. }
  5959. }
  5960. function symbolIteratorWithKey() {
  5961. if ((item = iter.next()).done === false) {
  5962. iterator(item.value, started, createCallback(started++));
  5963. }
  5964. }
  5965. function objectIterator() {
  5966. if (started < size) {
  5967. iterator(collection[keys[started]], createCallback(started++));
  5968. }
  5969. }
  5970. function objectIteratorWithKey() {
  5971. if (started < size) {
  5972. key = keys[started];
  5973. iterator(collection[key], key, createCallback(started++));
  5974. }
  5975. }
  5976. function createCallback(index) {
  5977. return function(err, res) {
  5978. if (index === null) {
  5979. throwError();
  5980. }
  5981. if (err) {
  5982. index = null;
  5983. iterate = noop;
  5984. callback = once(callback);
  5985. arrayEachSync(result, function(array, index) {
  5986. if (array === undefined) {
  5987. result[index] = noop;
  5988. }
  5989. });
  5990. callback(err, makeConcatResult(result));
  5991. return;
  5992. }
  5993. switch (arguments.length) {
  5994. case 0:
  5995. case 1:
  5996. result[index] = noop;
  5997. break;
  5998. case 2:
  5999. result[index] = res;
  6000. break;
  6001. default:
  6002. result[index] = slice(arguments, 1);
  6003. break;
  6004. }
  6005. index = null;
  6006. if (++completed === size) {
  6007. iterate = throwError;
  6008. callback(null, makeConcatResult(result));
  6009. callback = throwError;
  6010. } else if (sync) {
  6011. nextTick(iterate);
  6012. } else {
  6013. sync = true;
  6014. iterate();
  6015. }
  6016. sync = false;
  6017. };
  6018. }
  6019. }
  6020. /**
  6021. * @private
  6022. * @param {Function} arrayEach
  6023. * @param {Function} baseEach
  6024. * @param {Function} symbolEach
  6025. */
  6026. function createGroupBy(arrayEach, baseEach, symbolEach) {
  6027. return function groupBy(collection, iterator, callback) {
  6028. callback = callback || noop;
  6029. var size;
  6030. var completed = 0;
  6031. var result = {};
  6032. if (isArray(collection)) {
  6033. size = collection.length;
  6034. arrayEach(collection, iterator, createCallback);
  6035. } else if (!collection) {
  6036. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  6037. size = collection.size;
  6038. symbolEach(collection, iterator, createCallback);
  6039. } else if (typeof collection === obj) {
  6040. var keys = nativeKeys(collection);
  6041. size = keys.length;
  6042. baseEach(collection, iterator, createCallback, keys);
  6043. }
  6044. if (!size) {
  6045. callback(null, {});
  6046. }
  6047. function createCallback(value) {
  6048. var called = false;
  6049. return function done(err, key) {
  6050. if (called) {
  6051. throwError();
  6052. }
  6053. called = true;
  6054. if (err) {
  6055. callback = once(callback);
  6056. callback(err, objectClone(result));
  6057. return;
  6058. }
  6059. var array = result[key];
  6060. if (!array) {
  6061. array = result[key] = [value];
  6062. } else {
  6063. array.push(value);
  6064. }
  6065. if (++completed === size) {
  6066. callback(null, result);
  6067. }
  6068. };
  6069. }
  6070. };
  6071. }
  6072. /**
  6073. * @memberof async
  6074. * @namespace groupBySeries
  6075. * @param {Array|Object} collection
  6076. * @param {Function} iterator
  6077. * @param {Function} callback
  6078. * @example
  6079. *
  6080. * // array
  6081. * var order = [];
  6082. * var array = [4.2, 6.4, 6.1];
  6083. * var iterator = function(num, done) {
  6084. * setTimeout(function() {
  6085. * order.push(num);
  6086. * done(null, Math.floor(num));
  6087. * }, num * 10);
  6088. * };
  6089. * async.groupBySeries(array, iterator, function(err, res) {
  6090. * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
  6091. * console.log(order); // [4.2, 6.4, 6.1]
  6092. * });
  6093. *
  6094. * @example
  6095. *
  6096. * // array with index
  6097. * var order = [];
  6098. * var array = [4.2, 6.4, 6.1];
  6099. * var iterator = function(num, index, done) {
  6100. * setTimeout(function() {
  6101. * order.push([num, index]);
  6102. * done(null, Math.floor(num));
  6103. * }, num * 10);
  6104. * };
  6105. * async.groupBySeries(array, iterator, function(err, res) {
  6106. * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
  6107. * console.log(order); // [[4.2, 0], [6.4, 1], [6.1, 2]]
  6108. * });
  6109. *
  6110. * @example
  6111. *
  6112. * // object
  6113. * var order = [];
  6114. * var object = { a: 4.2, b: 6.4, c: 6.1 };
  6115. * var iterator = function(num, done) {
  6116. * setTimeout(function() {
  6117. * order.push(num);
  6118. * done(null, Math.floor(num));
  6119. * }, num * 10);
  6120. * };
  6121. * async.groupBySeries(object, iterator, function(err, res) {
  6122. * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
  6123. * console.log(order); // [4.2, 6.4, 6.1]
  6124. * });
  6125. *
  6126. * @example
  6127. *
  6128. * // object with key
  6129. * var order = [];
  6130. * var object = { a: 4.2, b: 6.4, c: 6.1 };
  6131. * var iterator = function(num, key, done) {
  6132. * setTimeout(function() {
  6133. * order.push([num, key]);
  6134. * done(null, Math.floor(num));
  6135. * }, num * 10);
  6136. * };
  6137. * async.groupBySeries(object, iterator, function(err, res) {
  6138. * console.log(res); // { '4': [4.2], '6': [6.4, 6.1] }
  6139. * console.log(order); // [[4.2, 'a'], [6.4, 'b'], [6.1, 'c']]
  6140. * });
  6141. *
  6142. */
  6143. function groupBySeries(collection, iterator, callback) {
  6144. callback = onlyOnce(callback || noop);
  6145. var size, key, value, keys, iter, iterate;
  6146. var sync = false;
  6147. var completed = 0;
  6148. var result = {};
  6149. if (isArray(collection)) {
  6150. size = collection.length;
  6151. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  6152. } else if (!collection) {
  6153. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  6154. size = collection.size;
  6155. iter = collection[iteratorSymbol]();
  6156. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  6157. } else if (typeof collection === obj) {
  6158. keys = nativeKeys(collection);
  6159. size = keys.length;
  6160. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  6161. }
  6162. if (!size) {
  6163. return callback(null, result);
  6164. }
  6165. iterate();
  6166. function arrayIterator() {
  6167. value = collection[completed];
  6168. iterator(value, done);
  6169. }
  6170. function arrayIteratorWithIndex() {
  6171. value = collection[completed];
  6172. iterator(value, completed, done);
  6173. }
  6174. function symbolIterator() {
  6175. value = iter.next().value;
  6176. iterator(value, done);
  6177. }
  6178. function symbolIteratorWithKey() {
  6179. value = iter.next().value;
  6180. iterator(value, completed, done);
  6181. }
  6182. function objectIterator() {
  6183. value = collection[keys[completed]];
  6184. iterator(value, done);
  6185. }
  6186. function objectIteratorWithKey() {
  6187. key = keys[completed];
  6188. value = collection[key];
  6189. iterator(value, key, done);
  6190. }
  6191. function done(err, key) {
  6192. if (err) {
  6193. iterate = throwError;
  6194. callback = onlyOnce(callback);
  6195. callback(err, objectClone(result));
  6196. return;
  6197. }
  6198. var array = result[key];
  6199. if (!array) {
  6200. array = result[key] = [value];
  6201. } else {
  6202. array.push(value);
  6203. }
  6204. if (++completed === size) {
  6205. iterate = throwError;
  6206. callback(null, result);
  6207. } else if (sync) {
  6208. nextTick(iterate);
  6209. } else {
  6210. sync = true;
  6211. iterate();
  6212. }
  6213. sync = false;
  6214. }
  6215. }
  6216. /**
  6217. * @memberof async
  6218. * @namespace groupByLimit
  6219. * @param {Array|Object} collection
  6220. * @param {Function} iterator
  6221. * @param {Function} callback
  6222. * @example
  6223. *
  6224. * // array
  6225. * var order = [];
  6226. * var array = [1.1, 5.9, 3.2, 3.9, 2.1];
  6227. * var iterator = function(num, done) {
  6228. * setTimeout(function() {
  6229. * order.push(num);
  6230. * done(null, Math.floor(num));
  6231. * }, num * 10);
  6232. * };
  6233. * async.groupByLimit(array, 2, iterator, function(err, res) {
  6234. * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  6235. * console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]
  6236. * });
  6237. *
  6238. * @example
  6239. *
  6240. * // array with index
  6241. * var order = [];
  6242. * var array = [1.1, 5.9, 3.2, 3.9, 2.1];
  6243. * var iterator = function(num, index, done) {
  6244. * setTimeout(function() {
  6245. * order.push([num, index]);
  6246. * done(null, Math.floor(num));
  6247. * }, num * 10);
  6248. * };
  6249. * async.groupByLimit(array, 2, iterator, function(err, res) {
  6250. * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  6251. * console.log(order); // [[1.1, 0], [3.2, 2], [5.9, 1], [2.1, 4], [3.9, 3]]
  6252. * });
  6253. *
  6254. * @example
  6255. *
  6256. * // object
  6257. * var order = [];
  6258. * var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }
  6259. * var iterator = function(num, done) {
  6260. * setTimeout(function() {
  6261. * order.push(num);
  6262. * done(null, Math.floor(num));
  6263. * }, num * 10);
  6264. * };
  6265. * async.groupByLimit(object, 2, iterator, function(err, res) {
  6266. * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  6267. * console.log(order); // [1.1, 3.2, 5.9, 2.1, 3.9]
  6268. * });
  6269. *
  6270. * @example
  6271. *
  6272. * // object with key
  6273. * var order = [];
  6274. * var object = { a: 1.1, b: 5.9, c: 3.2, d: 3.9, e: 2.1 }
  6275. * var iterator = function(num, key, done) {
  6276. * setTimeout(function() {
  6277. * order.push([num, key]);
  6278. * done(null, Math.floor(num));
  6279. * }, num * 10);
  6280. * };
  6281. * async.groupByLimit(object, 2, iterator, function(err, res) {
  6282. * console.log(res); // { '1': [1.1], '3': [3.2, 3.9], '5': [5.9], '2': [2.1] }
  6283. * console.log(order); // [[1.1, 'a'], [3.2, 'c'], [5.9, 'b'], [2.1, 'e'], [3.9, 'd']]
  6284. * });
  6285. *
  6286. */
  6287. function groupByLimit(collection, limit, iterator, callback) {
  6288. callback = callback || noop;
  6289. var size, index, key, value, keys, iter, item, iterate;
  6290. var sync = false;
  6291. var started = 0;
  6292. var completed = 0;
  6293. var result = {};
  6294. if (isArray(collection)) {
  6295. size = collection.length;
  6296. iterate = iterator.length === 3 ? arrayIteratorWithIndex : arrayIterator;
  6297. } else if (!collection) {
  6298. } else if (iteratorSymbol && collection[iteratorSymbol]) {
  6299. size = collection.size;
  6300. iter = collection[iteratorSymbol]();
  6301. iterate = iterator.length === 3 ? symbolIteratorWithKey : symbolIterator;
  6302. } else if (typeof collection === obj) {
  6303. keys = nativeKeys(collection);
  6304. size = keys.length;
  6305. iterate = iterator.length === 3 ? objectIteratorWithKey : objectIterator;
  6306. }
  6307. if (!size || isNaN(limit) || limit < 1) {
  6308. return callback(null, result);
  6309. }
  6310. timesSync(limit > size ? size : limit, iterate);
  6311. function arrayIterator() {
  6312. if (started < size) {
  6313. value = collection[started++];
  6314. iterator(value, createCallback(value));
  6315. }
  6316. }
  6317. function arrayIteratorWithIndex() {
  6318. index = started++;
  6319. if (index < size) {
  6320. value = collection[index];
  6321. iterator(value, index, createCallback(value));
  6322. }
  6323. }
  6324. function symbolIterator() {
  6325. if ((item = iter.next()).done === false) {
  6326. value = item.value;
  6327. iterator(value, createCallback(value));
  6328. }
  6329. }
  6330. function symbolIteratorWithKey() {
  6331. if ((item = iter.next()).done === false) {
  6332. value = item.value;
  6333. iterator(value, started++, createCallback(value));
  6334. }
  6335. }
  6336. function objectIterator() {
  6337. if (started < size) {
  6338. value = collection[keys[started++]];
  6339. iterator(value, createCallback(value));
  6340. }
  6341. }
  6342. function objectIteratorWithKey() {
  6343. if (started < size) {
  6344. key = keys[started++];
  6345. value = collection[key];
  6346. iterator(value, key, createCallback(value));
  6347. }
  6348. }
  6349. function createCallback(value) {
  6350. var called = false;
  6351. return function(err, key) {
  6352. if (called) {
  6353. throwError();
  6354. }
  6355. called = true;
  6356. if (err) {
  6357. iterate = noop;
  6358. callback = once(callback);
  6359. callback(err, objectClone(result));
  6360. return;
  6361. }
  6362. var array = result[key];
  6363. if (!array) {
  6364. result[key] = [value];
  6365. } else {
  6366. array.push(value);
  6367. }
  6368. if (++completed === size) {
  6369. callback(null, result);
  6370. } else if (sync) {
  6371. nextTick(iterate);
  6372. } else {
  6373. sync = true;
  6374. iterate();
  6375. }
  6376. sync = false;
  6377. };
  6378. }
  6379. }
  6380. /**
  6381. * @private
  6382. * @param {Function} arrayEach
  6383. * @param {Function} baseEach
  6384. */
  6385. function createParallel(arrayEach, baseEach) {
  6386. return function parallel(tasks, callback) {
  6387. callback = callback || noop;
  6388. var size, keys, result;
  6389. var completed = 0;
  6390. if (isArray(tasks)) {
  6391. size = tasks.length;
  6392. result = Array(size);
  6393. arrayEach(tasks, createCallback);
  6394. } else if (tasks && typeof tasks === obj) {
  6395. keys = nativeKeys(tasks);
  6396. size = keys.length;
  6397. result = {};
  6398. baseEach(tasks, createCallback, keys);
  6399. }
  6400. if (!size) {
  6401. callback(null, result);
  6402. }
  6403. function createCallback(key) {
  6404. return function(err, res) {
  6405. if (key === null) {
  6406. throwError();
  6407. }
  6408. if (err) {
  6409. key = null;
  6410. callback = once(callback);
  6411. callback(err, result);
  6412. return;
  6413. }
  6414. result[key] = arguments.length <= 2 ? res : slice(arguments, 1);
  6415. key = null;
  6416. if (++completed === size) {
  6417. callback(null, result);
  6418. }
  6419. };
  6420. }
  6421. };
  6422. }
  6423. /**
  6424. * @memberof async
  6425. * @namespace series
  6426. * @param {Array|Object} tasks - functions
  6427. * @param {Function} callback
  6428. * @example
  6429. *
  6430. * var order = [];
  6431. * var tasks = [
  6432. * function(done) {
  6433. * setTimeout(function() {
  6434. * order.push(1);
  6435. * done(null, 1);
  6436. * }, 10);
  6437. * },
  6438. * function(done) {
  6439. * setTimeout(function() {
  6440. * order.push(2);
  6441. * done(null, 2);
  6442. * }, 30);
  6443. * },
  6444. * function(done) {
  6445. * setTimeout(function() {
  6446. * order.push(3);
  6447. * done(null, 3);
  6448. * }, 40);
  6449. * },
  6450. * function(done) {
  6451. * setTimeout(function() {
  6452. * order.push(4);
  6453. * done(null, 4);
  6454. * }, 20);
  6455. * }
  6456. * ];
  6457. * async.series(tasks, function(err, res) {
  6458. * console.log(res); // [1, 2, 3, 4];
  6459. * console.log(order); // [1, 2, 3, 4]
  6460. * });
  6461. *
  6462. * @example
  6463. *
  6464. * var order = [];
  6465. * var tasks = {
  6466. * 'a': function(done) {
  6467. * setTimeout(function() {
  6468. * order.push(1);
  6469. * done(null, 1);
  6470. * }, 10);
  6471. * },
  6472. * 'b': function(done) {
  6473. * setTimeout(function() {
  6474. * order.push(2);
  6475. * done(null, 2);
  6476. * }, 30);
  6477. * },
  6478. * 'c': function(done) {
  6479. * setTimeout(function() {
  6480. * order.push(3);
  6481. * done(null, 3);
  6482. * }, 40);
  6483. * },
  6484. * 'd': function(done) {
  6485. * setTimeout(function() {
  6486. * order.push(4);
  6487. * done(null, 4);
  6488. * }, 20);
  6489. * }
  6490. * };
  6491. * async.series(tasks, function(err, res) {
  6492. * console.log(res); // { a: 1, b: 2, c: 3, d:4 }
  6493. * console.log(order); // [1, 4, 2, 3]
  6494. * });
  6495. *
  6496. */
  6497. function series(tasks, callback) {
  6498. callback = callback || noop;
  6499. var size, key, keys, result, iterate;
  6500. var sync = false;
  6501. var completed = 0;
  6502. if (isArray(tasks)) {
  6503. size = tasks.length;
  6504. result = Array(size);
  6505. iterate = arrayIterator;
  6506. } else if (tasks && typeof tasks === obj) {
  6507. keys = nativeKeys(tasks);
  6508. size = keys.length;
  6509. result = {};
  6510. iterate = objectIterator;
  6511. } else {
  6512. return callback(null);
  6513. }
  6514. if (!size) {
  6515. return callback(null, result);
  6516. }
  6517. iterate();
  6518. function arrayIterator() {
  6519. key = completed;
  6520. tasks[completed](done);
  6521. }
  6522. function objectIterator() {
  6523. key = keys[completed];
  6524. tasks[key](done);
  6525. }
  6526. function done(err, res) {
  6527. if (err) {
  6528. iterate = throwError;
  6529. callback = onlyOnce(callback);
  6530. callback(err, result);
  6531. return;
  6532. }
  6533. result[key] = arguments.length <= 2 ? res : slice(arguments, 1);
  6534. if (++completed === size) {
  6535. iterate = throwError;
  6536. callback(null, result);
  6537. } else if (sync) {
  6538. nextTick(iterate);
  6539. } else {
  6540. sync = true;
  6541. iterate();
  6542. }
  6543. sync = false;
  6544. }
  6545. }
  6546. /**
  6547. * @memberof async
  6548. * @namespace parallelLimit
  6549. * @param {Array|Object} tasks - functions
  6550. * @param {number} limit - limit >= 1
  6551. * @param {Function} callback
  6552. * @example
  6553. *
  6554. * var order = [];
  6555. * var tasks = [
  6556. * function(done) {
  6557. * setTimeout(function() {
  6558. * order.push(1);
  6559. * done(null, 1);
  6560. * }, 10);
  6561. * },
  6562. * function(done) {
  6563. * setTimeout(function() {
  6564. * order.push(2);
  6565. * done(null, 2);
  6566. * }, 50);
  6567. * },
  6568. * function(done) {
  6569. * setTimeout(function() {
  6570. * order.push(3);
  6571. * done(null, 3);
  6572. * }, 30);
  6573. * },
  6574. * function(done) {
  6575. * setTimeout(function() {
  6576. * order.push(4);
  6577. * done(null, 4);
  6578. * }, 40);
  6579. * }
  6580. * ];
  6581. * async.parallelLimit(tasks, 2, function(err, res) {
  6582. * console.log(res); // [1, 2, 3, 4];
  6583. * console.log(order); // [1, 3, 2, 4]
  6584. * });
  6585. *
  6586. * @example
  6587. *
  6588. * var order = [];
  6589. * var tasks = {
  6590. * 'a': function(done) {
  6591. * setTimeout(function() {
  6592. * order.push(1);
  6593. * done(null, 1);
  6594. * }, 10);
  6595. * },
  6596. * 'b': function(done) {
  6597. * setTimeout(function() {
  6598. * order.push(2);
  6599. * done(null, 2);
  6600. * }, 50);
  6601. * },
  6602. * 'c': function(done) {
  6603. * setTimeout(function() {
  6604. * order.push(3);
  6605. * done(null, 3);
  6606. * }, 20);
  6607. * },
  6608. * 'd': function(done) {
  6609. * setTimeout(function() {
  6610. * order.push(4);
  6611. * done(null, 4);
  6612. * }, 40);
  6613. * }
  6614. * };
  6615. * async.parallelLimit(tasks, 2, function(err, res) {
  6616. * console.log(res); // { a: 1, b: 2, c: 3, d:4 }
  6617. * console.log(order); // [1, 3, 2, 4]
  6618. * });
  6619. *
  6620. */
  6621. function parallelLimit(tasks, limit, callback) {
  6622. callback = callback || noop;
  6623. var size, index, key, keys, result, iterate;
  6624. var sync = false;
  6625. var started = 0;
  6626. var completed = 0;
  6627. if (isArray(tasks)) {
  6628. size = tasks.length;
  6629. result = Array(size);
  6630. iterate = arrayIterator;
  6631. } else if (tasks && typeof tasks === obj) {
  6632. keys = nativeKeys(tasks);
  6633. size = keys.length;
  6634. result = {};
  6635. iterate = objectIterator;
  6636. }
  6637. if (!size || isNaN(limit) || limit < 1) {
  6638. return callback(null, result);
  6639. }
  6640. timesSync(limit > size ? size : limit, iterate);
  6641. function arrayIterator() {
  6642. index = started++;
  6643. if (index < size) {
  6644. tasks[index](createCallback(index));
  6645. }
  6646. }
  6647. function objectIterator() {
  6648. if (started < size) {
  6649. key = keys[started++];
  6650. tasks[key](createCallback(key));
  6651. }
  6652. }
  6653. function createCallback(key) {
  6654. return function(err, res) {
  6655. if (key === null) {
  6656. throwError();
  6657. }
  6658. if (err) {
  6659. key = null;
  6660. iterate = noop;
  6661. callback = once(callback);
  6662. callback(err, result);
  6663. return;
  6664. }
  6665. result[key] = arguments.length <= 2 ? res : slice(arguments, 1);
  6666. key = null;
  6667. if (++completed === size) {
  6668. callback(null, result);
  6669. } else if (sync) {
  6670. nextTick(iterate);
  6671. } else {
  6672. sync = true;
  6673. iterate();
  6674. }
  6675. sync = false;
  6676. };
  6677. }
  6678. }
  6679. /**
  6680. * @memberof async
  6681. * @namespace tryEach
  6682. * @param {Array|Object} tasks - functions
  6683. * @param {Function} callback
  6684. * @example
  6685. *
  6686. * var tasks = [
  6687. * function(done) {
  6688. * setTimeout(function() {
  6689. * done(new Error('error'));
  6690. * }, 10);
  6691. * },
  6692. * function(done) {
  6693. * setTimeout(function() {
  6694. * done(null, 2);
  6695. * }, 10);
  6696. * }
  6697. * ];
  6698. * async.tryEach(tasks, function(err, res) {
  6699. * console.log(res); // 2
  6700. * });
  6701. *
  6702. * @example
  6703. *
  6704. * var tasks = [
  6705. * function(done) {
  6706. * setTimeout(function() {
  6707. * done(new Error('error1'));
  6708. * }, 10);
  6709. * },
  6710. * function(done) {
  6711. * setTimeout(function() {
  6712. * done(new Error('error2');
  6713. * }, 10);
  6714. * }
  6715. * ];
  6716. * async.tryEach(tasks, function(err, res) {
  6717. * console.log(err); // error2
  6718. * console.log(res); // undefined
  6719. * });
  6720. *
  6721. */
  6722. function tryEach(tasks, callback) {
  6723. callback = callback || noop;
  6724. var size, keys, iterate;
  6725. var sync = false;
  6726. var completed = 0;
  6727. if (isArray(tasks)) {
  6728. size = tasks.length;
  6729. iterate = arrayIterator;
  6730. } else if (tasks && typeof tasks === obj) {
  6731. keys = nativeKeys(tasks);
  6732. size = keys.length;
  6733. iterate = objectIterator;
  6734. }
  6735. if (!size) {
  6736. return callback(null);
  6737. }
  6738. iterate();
  6739. function arrayIterator() {
  6740. tasks[completed](done);
  6741. }
  6742. function objectIterator() {
  6743. tasks[keys[completed]](done);
  6744. }
  6745. function done(err, res) {
  6746. if (!err) {
  6747. if (arguments.length <= 2) {
  6748. callback(null, res);
  6749. } else {
  6750. callback(null, slice(arguments, 1));
  6751. }
  6752. } else if (++completed === size) {
  6753. callback(err);
  6754. } else {
  6755. sync = true;
  6756. iterate();
  6757. }
  6758. sync = false;
  6759. }
  6760. }
  6761. /**
  6762. * check for waterfall tasks
  6763. * @private
  6764. * @param {Array} tasks
  6765. * @param {Function} callback
  6766. * @return {boolean}
  6767. */
  6768. function checkWaterfallTasks(tasks, callback) {
  6769. if (!isArray(tasks)) {
  6770. callback(new Error('First argument to waterfall must be an array of functions'));
  6771. return false;
  6772. }
  6773. if (tasks.length === 0) {
  6774. callback(null);
  6775. return false;
  6776. }
  6777. return true;
  6778. }
  6779. /**
  6780. * check for waterfall tasks
  6781. * @private
  6782. * @param {function} func
  6783. * @param {Array|Object} args - arguments
  6784. * @return {function} next
  6785. */
  6786. function waterfallIterator(func, args, next) {
  6787. switch (args.length) {
  6788. case 0:
  6789. case 1:
  6790. return func(next);
  6791. case 2:
  6792. return func(args[1], next);
  6793. case 3:
  6794. return func(args[1], args[2], next);
  6795. case 4:
  6796. return func(args[1], args[2], args[3], next);
  6797. case 5:
  6798. return func(args[1], args[2], args[3], args[4], next);
  6799. case 6:
  6800. return func(args[1], args[2], args[3], args[4], args[5], next);
  6801. default:
  6802. args = slice(args, 1);
  6803. args.push(next);
  6804. return func.apply(null, args);
  6805. }
  6806. }
  6807. /**
  6808. * @memberof async
  6809. * @namespace waterfall
  6810. * @param {Array} tasks - functions
  6811. * @param {Function} callback
  6812. * @example
  6813. *
  6814. * var order = [];
  6815. * var tasks = [
  6816. * function(next) {
  6817. * setTimeout(function() {
  6818. * order.push(1);
  6819. * next(null, 1);
  6820. * }, 10);
  6821. * },
  6822. * function(arg1, next) {
  6823. * setTimeout(function() {
  6824. * order.push(2);
  6825. * next(null, 1, 2);
  6826. * }, 30);
  6827. * },
  6828. * function(arg1, arg2, next) {
  6829. * setTimeout(function() {
  6830. * order.push(3);
  6831. * next(null, 3);
  6832. * }, 20);
  6833. * },
  6834. * function(arg1, next) {
  6835. * setTimeout(function() {
  6836. * order.push(4);
  6837. * next(null, 1, 2, 3, 4);
  6838. * }, 40);
  6839. * }
  6840. * ];
  6841. * async.waterfall(tasks, function(err, arg1, arg2, arg3, arg4) {
  6842. * console.log(arg1, arg2, arg3, arg4); // 1 2 3 4
  6843. * });
  6844. *
  6845. */
  6846. function waterfall(tasks, callback) {
  6847. callback = callback || noop;
  6848. if (!checkWaterfallTasks(tasks, callback)) {
  6849. return;
  6850. }
  6851. var func, args, done, sync;
  6852. var completed = 0;
  6853. var size = tasks.length;
  6854. waterfallIterator(tasks[0], [], createCallback(0));
  6855. function iterate() {
  6856. waterfallIterator(func, args, createCallback(func));
  6857. }
  6858. function createCallback(index) {
  6859. return function next(err, res) {
  6860. if (index === undefined) {
  6861. callback = noop;
  6862. throwError();
  6863. }
  6864. index = undefined;
  6865. if (err) {
  6866. done = callback;
  6867. callback = throwError;
  6868. done(err);
  6869. return;
  6870. }
  6871. if (++completed === size) {
  6872. done = callback;
  6873. callback = throwError;
  6874. if (arguments.length <= 2) {
  6875. done(err, res);
  6876. } else {
  6877. done.apply(null, createArray(arguments));
  6878. }
  6879. return;
  6880. }
  6881. if (sync) {
  6882. args = arguments;
  6883. func = tasks[completed] || throwError;
  6884. nextTick(iterate);
  6885. } else {
  6886. sync = true;
  6887. waterfallIterator(tasks[completed] || throwError, arguments, createCallback(completed));
  6888. }
  6889. sync = false;
  6890. };
  6891. }
  6892. }
  6893. /**
  6894. * `angelFall` is like `waterfall` and inject callback to last argument of next task.
  6895. *
  6896. * @memberof async
  6897. * @namespace angelFall
  6898. * @param {Array} tasks - functions
  6899. * @param {Function} callback
  6900. * @example
  6901. *
  6902. * var order = [];
  6903. * var tasks = [
  6904. * function(next) {
  6905. * setTimeout(function() {
  6906. * order.push(1);
  6907. * next(null, 1);
  6908. * }, 10);
  6909. * },
  6910. * function(arg1, empty, next) {
  6911. * setTimeout(function() {
  6912. * order.push(2);
  6913. * next(null, 1, 2);
  6914. * }, 30);
  6915. * },
  6916. * function(next) {
  6917. * setTimeout(function() {
  6918. * order.push(3);
  6919. * next(null, 3);
  6920. * }, 20);
  6921. * },
  6922. * function(arg1, empty1, empty2, empty3, next) {
  6923. * setTimeout(function() {
  6924. * order.push(4);
  6925. * next(null, 1, 2, 3, 4);
  6926. * }, 40);
  6927. * }
  6928. * ];
  6929. * async.angelFall(tasks, function(err, arg1, arg2, arg3, arg4) {
  6930. * console.log(arg1, arg2, arg3, arg4); // 1 2 3 4
  6931. * });
  6932. *
  6933. */
  6934. function angelFall(tasks, callback) {
  6935. callback = callback || noop;
  6936. if (!checkWaterfallTasks(tasks, callback)) {
  6937. return;
  6938. }
  6939. var completed = 0;
  6940. var sync = false;
  6941. var size = tasks.length;
  6942. var func = tasks[completed];
  6943. var args = [];
  6944. var iterate = function() {
  6945. switch (func.length) {
  6946. case 0:
  6947. try {
  6948. next(null, func());
  6949. } catch (e) {
  6950. next(e);
  6951. }
  6952. return;
  6953. case 1:
  6954. return func(next);
  6955. case 2:
  6956. return func(args[1], next);
  6957. case 3:
  6958. return func(args[1], args[2], next);
  6959. case 4:
  6960. return func(args[1], args[2], args[3], next);
  6961. case 5:
  6962. return func(args[1], args[2], args[3], args[4], next);
  6963. default:
  6964. args = slice(args, 1);
  6965. args[func.length - 1] = next;
  6966. return func.apply(null, args);
  6967. }
  6968. };
  6969. iterate();
  6970. function next(err, res) {
  6971. if (err) {
  6972. iterate = throwError;
  6973. callback = onlyOnce(callback);
  6974. callback(err);
  6975. return;
  6976. }
  6977. if (++completed === size) {
  6978. iterate = throwError;
  6979. var done = callback;
  6980. callback = throwError;
  6981. if (arguments.length === 2) {
  6982. done(err, res);
  6983. } else {
  6984. done.apply(null, createArray(arguments));
  6985. }
  6986. return;
  6987. }
  6988. func = tasks[completed];
  6989. args = arguments;
  6990. if (sync) {
  6991. nextTick(iterate);
  6992. } else {
  6993. sync = true;
  6994. iterate();
  6995. }
  6996. sync = false;
  6997. }
  6998. }
  6999. /**
  7000. * @memberof async
  7001. * @namespace whilst
  7002. * @param {Function} test
  7003. * @param {Function} iterator
  7004. * @param {Function} callback
  7005. */
  7006. function whilst(test, iterator, callback) {
  7007. callback = callback || noop;
  7008. var sync = false;
  7009. if (test()) {
  7010. iterate();
  7011. } else {
  7012. callback(null);
  7013. }
  7014. function iterate() {
  7015. if (sync) {
  7016. nextTick(next);
  7017. } else {
  7018. sync = true;
  7019. iterator(done);
  7020. }
  7021. sync = false;
  7022. }
  7023. function next() {
  7024. iterator(done);
  7025. }
  7026. function done(err, arg) {
  7027. if (err) {
  7028. return callback(err);
  7029. }
  7030. if (arguments.length <= 2) {
  7031. if (test(arg)) {
  7032. iterate();
  7033. } else {
  7034. callback(null, arg);
  7035. }
  7036. return;
  7037. }
  7038. arg = slice(arguments, 1);
  7039. if (test.apply(null, arg)) {
  7040. iterate();
  7041. } else {
  7042. callback.apply(null, [null].concat(arg));
  7043. }
  7044. }
  7045. }
  7046. /**
  7047. * @memberof async
  7048. * @namespace doWhilst
  7049. * @param {Function} iterator
  7050. * @param {Function} test
  7051. * @param {Function} callback
  7052. */
  7053. function doWhilst(iterator, test, callback) {
  7054. callback = callback || noop;
  7055. var sync = false;
  7056. next();
  7057. function iterate() {
  7058. if (sync) {
  7059. nextTick(next);
  7060. } else {
  7061. sync = true;
  7062. iterator(done);
  7063. }
  7064. sync = false;
  7065. }
  7066. function next() {
  7067. iterator(done);
  7068. }
  7069. function done(err, arg) {
  7070. if (err) {
  7071. return callback(err);
  7072. }
  7073. if (arguments.length <= 2) {
  7074. if (test(arg)) {
  7075. iterate();
  7076. } else {
  7077. callback(null, arg);
  7078. }
  7079. return;
  7080. }
  7081. arg = slice(arguments, 1);
  7082. if (test.apply(null, arg)) {
  7083. iterate();
  7084. } else {
  7085. callback.apply(null, [null].concat(arg));
  7086. }
  7087. }
  7088. }
  7089. /**
  7090. * @memberof async
  7091. * @namespace until
  7092. * @param {Function} test
  7093. * @param {Function} iterator
  7094. * @param {Function} callback
  7095. */
  7096. function until(test, iterator, callback) {
  7097. callback = callback || noop;
  7098. var sync = false;
  7099. if (!test()) {
  7100. iterate();
  7101. } else {
  7102. callback(null);
  7103. }
  7104. function iterate() {
  7105. if (sync) {
  7106. nextTick(next);
  7107. } else {
  7108. sync = true;
  7109. iterator(done);
  7110. }
  7111. sync = false;
  7112. }
  7113. function next() {
  7114. iterator(done);
  7115. }
  7116. function done(err, arg) {
  7117. if (err) {
  7118. return callback(err);
  7119. }
  7120. if (arguments.length <= 2) {
  7121. if (!test(arg)) {
  7122. iterate();
  7123. } else {
  7124. callback(null, arg);
  7125. }
  7126. return;
  7127. }
  7128. arg = slice(arguments, 1);
  7129. if (!test.apply(null, arg)) {
  7130. iterate();
  7131. } else {
  7132. callback.apply(null, [null].concat(arg));
  7133. }
  7134. }
  7135. }
  7136. /**
  7137. * @memberof async
  7138. * @namespace doUntil
  7139. * @param {Function} iterator
  7140. * @param {Function} test
  7141. * @param {Function} callback
  7142. */
  7143. function doUntil(iterator, test, callback) {
  7144. callback = callback || noop;
  7145. var sync = false;
  7146. next();
  7147. function iterate() {
  7148. if (sync) {
  7149. nextTick(next);
  7150. } else {
  7151. sync = true;
  7152. iterator(done);
  7153. }
  7154. sync = false;
  7155. }
  7156. function next() {
  7157. iterator(done);
  7158. }
  7159. function done(err, arg) {
  7160. if (err) {
  7161. return callback(err);
  7162. }
  7163. if (arguments.length <= 2) {
  7164. if (!test(arg)) {
  7165. iterate();
  7166. } else {
  7167. callback(null, arg);
  7168. }
  7169. return;
  7170. }
  7171. arg = slice(arguments, 1);
  7172. if (!test.apply(null, arg)) {
  7173. iterate();
  7174. } else {
  7175. callback.apply(null, [null].concat(arg));
  7176. }
  7177. }
  7178. }
  7179. /**
  7180. * @memberof async
  7181. * @namespace during
  7182. * @param {Function} test
  7183. * @param {Function} iterator
  7184. * @param {Function} callback
  7185. */
  7186. function during(test, iterator, callback) {
  7187. callback = callback || noop;
  7188. _test();
  7189. function _test() {
  7190. test(iterate);
  7191. }
  7192. function iterate(err, truth) {
  7193. if (err) {
  7194. return callback(err);
  7195. }
  7196. if (truth) {
  7197. iterator(done);
  7198. } else {
  7199. callback(null);
  7200. }
  7201. }
  7202. function done(err) {
  7203. if (err) {
  7204. return callback(err);
  7205. }
  7206. _test();
  7207. }
  7208. }
  7209. /**
  7210. * @memberof async
  7211. * @namespace doDuring
  7212. * @param {Function} test
  7213. * @param {Function} iterator
  7214. * @param {Function} callback
  7215. */
  7216. function doDuring(iterator, test, callback) {
  7217. callback = callback || noop;
  7218. iterate(null, true);
  7219. function iterate(err, truth) {
  7220. if (err) {
  7221. return callback(err);
  7222. }
  7223. if (truth) {
  7224. iterator(done);
  7225. } else {
  7226. callback(null);
  7227. }
  7228. }
  7229. function done(err, res) {
  7230. if (err) {
  7231. return callback(err);
  7232. }
  7233. switch (arguments.length) {
  7234. case 0:
  7235. case 1:
  7236. test(iterate);
  7237. break;
  7238. case 2:
  7239. test(res, iterate);
  7240. break;
  7241. default:
  7242. var args = slice(arguments, 1);
  7243. args.push(iterate);
  7244. test.apply(null, args);
  7245. break;
  7246. }
  7247. }
  7248. }
  7249. /**
  7250. * @memberof async
  7251. * @namespace forever
  7252. */
  7253. function forever(iterator, callback) {
  7254. var sync = false;
  7255. iterate();
  7256. function iterate() {
  7257. iterator(next);
  7258. }
  7259. function next(err) {
  7260. if (err) {
  7261. if (callback) {
  7262. return callback(err);
  7263. }
  7264. throw err;
  7265. }
  7266. if (sync) {
  7267. nextTick(iterate);
  7268. } else {
  7269. sync = true;
  7270. iterate();
  7271. }
  7272. sync = false;
  7273. }
  7274. }
  7275. /**
  7276. * @memberof async
  7277. * @namespace compose
  7278. */
  7279. function compose() {
  7280. return seq.apply(null, reverse(arguments));
  7281. }
  7282. /**
  7283. * @memberof async
  7284. * @namespace seq
  7285. */
  7286. function seq( /* functions... */ ) {
  7287. var fns = createArray(arguments);
  7288. return function() {
  7289. var self = this;
  7290. var args = createArray(arguments);
  7291. var callback = args[args.length - 1];
  7292. if (typeof callback === func) {
  7293. args.pop();
  7294. } else {
  7295. callback = noop;
  7296. }
  7297. reduce(fns, args, iterator, done);
  7298. function iterator(newargs, fn, callback) {
  7299. var func = function(err) {
  7300. var nextargs = slice(arguments, 1);
  7301. callback(err, nextargs);
  7302. };
  7303. newargs.push(func);
  7304. fn.apply(self, newargs);
  7305. }
  7306. function done(err, res) {
  7307. res = isArray(res) ? res : [res];
  7308. res.unshift(err);
  7309. callback.apply(self, res);
  7310. }
  7311. };
  7312. }
  7313. function createApplyEach(func) {
  7314. return function applyEach(fns /* arguments */ ) {
  7315. var go = function() {
  7316. var self = this;
  7317. var args = createArray(arguments);
  7318. var callback = args.pop() || noop;
  7319. return func(fns, iterator, callback);
  7320. function iterator(fn, done) {
  7321. fn.apply(self, args.concat([done]));
  7322. }
  7323. };
  7324. if (arguments.length > 1) {
  7325. var args = slice(arguments, 1);
  7326. return go.apply(this, args);
  7327. } else {
  7328. return go;
  7329. }
  7330. };
  7331. }
  7332. /**
  7333. * @see https://github.com/caolan/async/blob/master/lib/internal/DoublyLinkedList.js
  7334. */
  7335. function DLL() {
  7336. this.head = null;
  7337. this.tail = null;
  7338. this.length = 0;
  7339. }
  7340. DLL.prototype._removeLink = function(node) {
  7341. var prev = node.prev;
  7342. var next = node.next;
  7343. if (prev) {
  7344. prev.next = next;
  7345. } else {
  7346. this.head = next;
  7347. }
  7348. if (next) {
  7349. next.prev = prev;
  7350. } else {
  7351. this.tail = prev;
  7352. }
  7353. node.prev = null;
  7354. node.next = null;
  7355. this.length--;
  7356. return node;
  7357. };
  7358. DLL.prototype.empty = DLL;
  7359. DLL.prototype._setInitial = function(node) {
  7360. this.length = 1;
  7361. this.head = this.tail = node;
  7362. };
  7363. DLL.prototype.insertBefore = function(node, newNode) {
  7364. newNode.prev = node.prev;
  7365. newNode.next = node;
  7366. if (node.prev) {
  7367. node.prev.next = newNode;
  7368. } else {
  7369. this.head = newNode;
  7370. }
  7371. node.prev = newNode;
  7372. this.length++;
  7373. };
  7374. DLL.prototype.unshift = function(node) {
  7375. if (this.head) {
  7376. this.insertBefore(this.head, node);
  7377. } else {
  7378. this._setInitial(node);
  7379. }
  7380. };
  7381. DLL.prototype.push = function(node) {
  7382. var tail = this.tail;
  7383. if (tail) {
  7384. node.prev = tail;
  7385. node.next = tail.next;
  7386. this.tail = node;
  7387. tail.next = node;
  7388. this.length++;
  7389. } else {
  7390. this._setInitial(node);
  7391. }
  7392. };
  7393. DLL.prototype.shift = function() {
  7394. return this.head && this._removeLink(this.head);
  7395. };
  7396. DLL.prototype.splice = function(end) {
  7397. var task;
  7398. var tasks = [];
  7399. while (end-- && (task = this.shift())) {
  7400. tasks.push(task);
  7401. }
  7402. return tasks;
  7403. };
  7404. DLL.prototype.remove = function(test) {
  7405. var node = this.head;
  7406. while(node) {
  7407. if (test(node)) {
  7408. this._removeLink(node);
  7409. }
  7410. node = node.next;
  7411. }
  7412. return this;
  7413. };
  7414. /**
  7415. * @private
  7416. */
  7417. function baseQueue(isQueue, worker, concurrency, payload) {
  7418. if (concurrency === undefined) {
  7419. concurrency = 1;
  7420. } else if (isNaN(concurrency) || concurrency < 1) {
  7421. throw new Error('Concurrency must not be zero');
  7422. }
  7423. var workers = 0;
  7424. var workersList = [];
  7425. var _callback, _unshift;
  7426. var q = {
  7427. _tasks: new DLL(),
  7428. concurrency: concurrency,
  7429. payload: payload,
  7430. saturated: noop,
  7431. unsaturated: noop,
  7432. buffer: concurrency / 4,
  7433. empty: noop,
  7434. drain: noop,
  7435. error: noop,
  7436. started: false,
  7437. paused: false,
  7438. push: push,
  7439. kill: kill,
  7440. unshift: unshift,
  7441. remove: remove,
  7442. process: isQueue ? runQueue : runCargo,
  7443. length: getLength,
  7444. running: running,
  7445. workersList: getWorkersList,
  7446. idle: idle,
  7447. pause: pause,
  7448. resume: resume,
  7449. _worker: worker
  7450. };
  7451. return q;
  7452. function push(tasks, callback) {
  7453. _insert(tasks, callback);
  7454. }
  7455. function unshift(tasks, callback) {
  7456. _insert(tasks, callback, true);
  7457. }
  7458. function _exec(task) {
  7459. var item = {
  7460. data: task,
  7461. callback: _callback
  7462. };
  7463. if (_unshift) {
  7464. q._tasks.unshift(item);
  7465. } else {
  7466. q._tasks.push(item);
  7467. }
  7468. nextTick(q.process);
  7469. }
  7470. function _insert(tasks, callback, unshift) {
  7471. if (callback == null) {
  7472. callback = noop;
  7473. } else if (typeof callback !== 'function') {
  7474. throw new Error('task callback must be a function');
  7475. }
  7476. q.started = true;
  7477. var _tasks = isArray(tasks) ? tasks : [tasks];
  7478. if (tasks === undefined || !_tasks.length) {
  7479. if (q.idle()) {
  7480. nextTick(q.drain);
  7481. }
  7482. return;
  7483. }
  7484. _unshift = unshift;
  7485. _callback = callback;
  7486. arrayEachSync(_tasks, _exec);
  7487. }
  7488. function kill() {
  7489. q.drain = noop;
  7490. q._tasks.empty();
  7491. }
  7492. function _next(q, tasks) {
  7493. var called = false;
  7494. return function done(err, res) {
  7495. if (called) {
  7496. throwError();
  7497. }
  7498. called = true;
  7499. workers--;
  7500. var task;
  7501. var index = -1;
  7502. var size = workersList.length;
  7503. var taskIndex = -1;
  7504. var taskSize = tasks.length;
  7505. var useApply = arguments.length > 2;
  7506. var args = useApply && createArray(arguments);
  7507. while (++taskIndex < taskSize) {
  7508. task = tasks[taskIndex];
  7509. while (++index < size) {
  7510. if (workersList[index] === task) {
  7511. workersList.splice(index, 1);
  7512. index = size;
  7513. size--;
  7514. }
  7515. }
  7516. index = -1;
  7517. if (useApply) {
  7518. task.callback.apply(task, args);
  7519. } else {
  7520. task.callback(err, res);
  7521. }
  7522. if (err) {
  7523. q.error(err, task.data);
  7524. }
  7525. }
  7526. if (workers <= q.concurrency - q.buffer) {
  7527. q.unsaturated();
  7528. }
  7529. if (q._tasks.length + workers === 0) {
  7530. q.drain();
  7531. }
  7532. q.process();
  7533. };
  7534. }
  7535. function runQueue() {
  7536. while (!q.paused && workers < q.concurrency && q._tasks.length) {
  7537. var task = q._tasks.shift();
  7538. workers++;
  7539. workersList.push(task);
  7540. if (q._tasks.length === 0) {
  7541. q.empty();
  7542. }
  7543. if (workers === q.concurrency) {
  7544. q.saturated();
  7545. }
  7546. var done = _next(q, [task]);
  7547. worker(task.data, done);
  7548. }
  7549. }
  7550. function runCargo() {
  7551. while (!q.paused && workers < q.concurrency && q._tasks.length) {
  7552. var tasks = q._tasks.splice(q.payload || q._tasks.length);
  7553. var index = -1;
  7554. var size = tasks.length;
  7555. var data = Array(size);
  7556. while (++index < size) {
  7557. data[index] = tasks[index].data;
  7558. }
  7559. workers++;
  7560. nativePush.apply(workersList, tasks);
  7561. if (q._tasks.length === 0) {
  7562. q.empty();
  7563. }
  7564. if (workers === q.concurrency) {
  7565. q.saturated();
  7566. }
  7567. var done = _next(q, tasks);
  7568. worker(data, done);
  7569. }
  7570. }
  7571. function getLength() {
  7572. return q._tasks.length;
  7573. }
  7574. function running() {
  7575. return workers;
  7576. }
  7577. function getWorkersList() {
  7578. return workersList;
  7579. }
  7580. function idle() {
  7581. return q.length() + workers === 0;
  7582. }
  7583. function pause() {
  7584. q.paused = true;
  7585. }
  7586. function _resume() {
  7587. nextTick(q.process);
  7588. }
  7589. function resume() {
  7590. if (q.paused === false) {
  7591. return;
  7592. }
  7593. q.paused = false;
  7594. var count = q.concurrency < q._tasks.length ? q.concurrency : q._tasks.length;
  7595. timesSync(count, _resume);
  7596. }
  7597. /**
  7598. * @param {Function} test
  7599. */
  7600. function remove(test) {
  7601. q._tasks.remove(test);
  7602. }
  7603. }
  7604. /**
  7605. * @memberof async
  7606. * @namespace queue
  7607. */
  7608. function queue(worker, concurrency) {
  7609. return baseQueue(true, worker, concurrency);
  7610. }
  7611. /**
  7612. * @memberof async
  7613. * @namespace priorityQueue
  7614. */
  7615. function priorityQueue(worker, concurrency) {
  7616. var q = baseQueue(true, worker, concurrency);
  7617. q.push = push;
  7618. delete q.unshift;
  7619. return q;
  7620. function push(tasks, priority, callback) {
  7621. q.started = true;
  7622. priority = priority || 0;
  7623. var _tasks = isArray(tasks) ? tasks : [tasks];
  7624. var taskSize = _tasks.length;
  7625. if (tasks === undefined || taskSize === 0) {
  7626. if (q.idle()) {
  7627. nextTick(q.drain);
  7628. }
  7629. return;
  7630. }
  7631. callback = typeof callback === func ? callback : noop;
  7632. var nextNode = q._tasks.head;
  7633. while (nextNode && priority >= nextNode.priority) {
  7634. nextNode = nextNode.next;
  7635. }
  7636. while (taskSize--) {
  7637. var item = {
  7638. data: _tasks[taskSize],
  7639. priority: priority,
  7640. callback: callback
  7641. };
  7642. if (nextNode) {
  7643. q._tasks.insertBefore(nextNode, item);
  7644. } else {
  7645. q._tasks.push(item);
  7646. }
  7647. nextTick(q.process);
  7648. }
  7649. }
  7650. }
  7651. /**
  7652. * @memberof async
  7653. * @namespace cargo
  7654. */
  7655. function cargo(worker, payload) {
  7656. return baseQueue(false, worker, 1, payload);
  7657. }
  7658. /**
  7659. * @memberof async
  7660. * @namespace auto
  7661. * @param {Object} tasks
  7662. * @param {number} [concurrency]
  7663. * @param {Function} [callback]
  7664. */
  7665. function auto(tasks, concurrency, callback) {
  7666. if (typeof concurrency === func) {
  7667. callback = concurrency;
  7668. concurrency = null;
  7669. }
  7670. var keys = nativeKeys(tasks);
  7671. var rest = keys.length;
  7672. var results = {};
  7673. if (rest === 0) {
  7674. return callback(null, results);
  7675. }
  7676. var runningTasks = 0;
  7677. var readyTasks = [];
  7678. var listeners = Object.create(null);
  7679. callback = onlyOnce(callback || noop);
  7680. concurrency = concurrency || rest;
  7681. baseEachSync(tasks, iterator, keys);
  7682. proceedQueue();
  7683. function iterator(task, key) {
  7684. // no dependencies
  7685. var _task, _taskSize;
  7686. if (!isArray(task)) {
  7687. _task = task;
  7688. _taskSize = 0;
  7689. readyTasks.push([_task, _taskSize, done]);
  7690. return;
  7691. }
  7692. var dependencySize = task.length - 1;
  7693. _task = task[dependencySize];
  7694. _taskSize = dependencySize;
  7695. if (dependencySize === 0) {
  7696. readyTasks.push([_task, _taskSize, done]);
  7697. return;
  7698. }
  7699. // dependencies
  7700. var index = -1;
  7701. while (++index < dependencySize) {
  7702. var dependencyName = task[index];
  7703. if (notInclude(keys, dependencyName)) {
  7704. var msg = 'async.auto task `' + key + '` has non-existent dependency `' + dependencyName + '` in ' + task.join(', ');
  7705. throw new Error(msg);
  7706. }
  7707. var taskListeners = listeners[dependencyName];
  7708. if (!taskListeners) {
  7709. taskListeners = listeners[dependencyName] = [];
  7710. }
  7711. taskListeners.push(taskListener);
  7712. }
  7713. function done(err, arg) {
  7714. if (key === null) {
  7715. throwError();
  7716. }
  7717. arg = arguments.length <= 2 ? arg : slice(arguments, 1);
  7718. if (err) {
  7719. rest = 0;
  7720. runningTasks = 0;
  7721. readyTasks.length = 0;
  7722. var safeResults = objectClone(results);
  7723. safeResults[key] = arg;
  7724. key = null;
  7725. var _callback = callback;
  7726. callback = noop;
  7727. _callback(err, safeResults);
  7728. return;
  7729. }
  7730. runningTasks--;
  7731. rest--;
  7732. results[key] = arg;
  7733. taskComplete(key);
  7734. key = null;
  7735. }
  7736. function taskListener() {
  7737. if (--dependencySize === 0) {
  7738. readyTasks.push([_task, _taskSize, done]);
  7739. }
  7740. }
  7741. }
  7742. function proceedQueue() {
  7743. if (readyTasks.length === 0 && runningTasks === 0) {
  7744. if (rest !== 0) {
  7745. throw new Error('async.auto task has cyclic dependencies');
  7746. }
  7747. return callback(null, results);
  7748. }
  7749. while (readyTasks.length && runningTasks < concurrency && callback !== noop) {
  7750. runningTasks++;
  7751. var array = readyTasks.shift();
  7752. if (array[1] === 0) {
  7753. array[0](array[2]);
  7754. } else {
  7755. array[0](results, array[2]);
  7756. }
  7757. }
  7758. }
  7759. function taskComplete(key) {
  7760. var taskListeners = listeners[key] || [];
  7761. arrayEachSync(taskListeners, function(task) {
  7762. task();
  7763. });
  7764. proceedQueue();
  7765. }
  7766. }
  7767. var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m;
  7768. var FN_ARG_SPLIT = /,/;
  7769. var FN_ARG = /(=.+)?(\s*)$/;
  7770. var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
  7771. /**
  7772. * parse function arguments for `autoInject`
  7773. *
  7774. * @private
  7775. */
  7776. function parseParams(func) {
  7777. func = func.toString().replace(STRIP_COMMENTS, '');
  7778. func = func.match(FN_ARGS)[2].replace(' ', '');
  7779. func = func ? func.split(FN_ARG_SPLIT) : [];
  7780. func = func.map(function (arg) {
  7781. return arg.replace(FN_ARG, '').trim();
  7782. });
  7783. return func;
  7784. }
  7785. /**
  7786. * @memberof async
  7787. * @namespace autoInject
  7788. * @param {Object} tasks
  7789. * @param {number} [concurrency]
  7790. * @param {Function} [callback]
  7791. */
  7792. function autoInject(tasks, concurrency, callback) {
  7793. var newTasks = {};
  7794. baseEachSync(tasks, iterator, nativeKeys(tasks));
  7795. auto(newTasks, concurrency, callback);
  7796. function iterator(task, key) {
  7797. var params;
  7798. var taskLength = task.length;
  7799. if (isArray(task)) {
  7800. if (taskLength === 0) {
  7801. throw new Error('autoInject task functions require explicit parameters.');
  7802. }
  7803. params = createArray(task);
  7804. taskLength = params.length - 1;
  7805. task = params[taskLength];
  7806. if (taskLength === 0) {
  7807. newTasks[key] = task;
  7808. return;
  7809. }
  7810. } else if (taskLength === 1) {
  7811. newTasks[key] = task;
  7812. return;
  7813. } else {
  7814. params = parseParams(task);
  7815. if (taskLength === 0 && params.length === 0) {
  7816. throw new Error('autoInject task functions require explicit parameters.');
  7817. }
  7818. taskLength = params.length - 1;
  7819. }
  7820. params[taskLength] = newTask;
  7821. newTasks[key] = params;
  7822. function newTask(results, done) {
  7823. switch (taskLength) {
  7824. case 1:
  7825. task(results[params[0]], done);
  7826. break;
  7827. case 2:
  7828. task(results[params[0]], results[params[1]], done);
  7829. break;
  7830. case 3:
  7831. task(results[params[0]], results[params[1]], results[params[2]], done);
  7832. break;
  7833. default:
  7834. var i = -1;
  7835. while(++i < taskLength) {
  7836. params[i] = results[params[i]];
  7837. }
  7838. params[i] = done;
  7839. task.apply(null, params);
  7840. break;
  7841. }
  7842. }
  7843. }
  7844. }
  7845. /**
  7846. * @memberof async
  7847. * @namespace retry
  7848. * @param {integer|Object|Function} opts
  7849. * @param {Function} [task]
  7850. * @param {Function} [callback]
  7851. */
  7852. function retry(opts, task, callback) {
  7853. var times, intervalFunc, errorFilter;
  7854. var count = 0;
  7855. if (arguments.length < 3 && typeof opts === func) {
  7856. callback = task || noop;
  7857. task = opts;
  7858. opts = null;
  7859. times = DEFAULT_TIMES;
  7860. } else {
  7861. callback = callback || noop;
  7862. switch (typeof opts) {
  7863. case 'object':
  7864. if (typeof opts.errorFilter === func) {
  7865. errorFilter = opts.errorFilter;
  7866. }
  7867. var interval = opts.interval;
  7868. switch (typeof interval) {
  7869. case func:
  7870. intervalFunc = interval;
  7871. break;
  7872. case 'string':
  7873. case 'number':
  7874. interval = +interval;
  7875. intervalFunc = interval ? function() {
  7876. return interval;
  7877. } : function() {
  7878. return DEFAULT_INTERVAL;
  7879. };
  7880. break;
  7881. }
  7882. times = +opts.times || DEFAULT_TIMES;
  7883. break;
  7884. case 'number':
  7885. times = opts || DEFAULT_TIMES;
  7886. break;
  7887. case 'string':
  7888. times = +opts || DEFAULT_TIMES;
  7889. break;
  7890. default:
  7891. throw new Error('Invalid arguments for async.retry');
  7892. }
  7893. }
  7894. if (typeof task !== 'function') {
  7895. throw new Error('Invalid arguments for async.retry');
  7896. }
  7897. if (intervalFunc) {
  7898. task(intervalCallback);
  7899. } else {
  7900. task(simpleCallback);
  7901. }
  7902. function simpleIterator() {
  7903. task(simpleCallback);
  7904. }
  7905. function simpleCallback(err, res) {
  7906. if (++count === times || !err || errorFilter && !errorFilter(err)) {
  7907. if (arguments.length <= 2) {
  7908. return callback(err, res);
  7909. }
  7910. var args = createArray(arguments);
  7911. return callback.apply(null, args);
  7912. }
  7913. simpleIterator();
  7914. }
  7915. function intervalIterator() {
  7916. task(intervalCallback);
  7917. }
  7918. function intervalCallback(err, res) {
  7919. if (++count === times || !err || errorFilter && !errorFilter(err)) {
  7920. if (arguments.length <= 2) {
  7921. return callback(err, res);
  7922. }
  7923. var args = createArray(arguments);
  7924. return callback.apply(null, args);
  7925. }
  7926. setTimeout(intervalIterator, intervalFunc(count));
  7927. }
  7928. }
  7929. function retryable(opts, task) {
  7930. if (!task) {
  7931. task = opts;
  7932. opts = null;
  7933. }
  7934. return done;
  7935. function done() {
  7936. var taskFn;
  7937. var args = createArray(arguments);
  7938. var lastIndex = args.length - 1;
  7939. var callback = args[lastIndex];
  7940. switch (task.length) {
  7941. case 1:
  7942. taskFn = task1;
  7943. break;
  7944. case 2:
  7945. taskFn = task2;
  7946. break;
  7947. case 3:
  7948. taskFn = task3;
  7949. break;
  7950. default:
  7951. taskFn = task4;
  7952. }
  7953. if (opts) {
  7954. retry(opts, taskFn, callback);
  7955. } else {
  7956. retry(taskFn, callback);
  7957. }
  7958. function task1(done) {
  7959. task(done);
  7960. }
  7961. function task2(done) {
  7962. task(args[0], done);
  7963. }
  7964. function task3(done) {
  7965. task(args[0], args[1], done);
  7966. }
  7967. function task4(callback) {
  7968. args[lastIndex] = callback;
  7969. task.apply(null, args);
  7970. }
  7971. }
  7972. }
  7973. /**
  7974. * @memberof async
  7975. * @namespace iterator
  7976. */
  7977. function iterator(tasks) {
  7978. var size = 0;
  7979. var keys = [];
  7980. if (isArray(tasks)) {
  7981. size = tasks.length;
  7982. } else {
  7983. keys = nativeKeys(tasks);
  7984. size = keys.length;
  7985. }
  7986. return makeCallback(0);
  7987. function makeCallback(index) {
  7988. var fn = function() {
  7989. if (size) {
  7990. var key = keys[index] || index;
  7991. tasks[key].apply(null, createArray(arguments));
  7992. }
  7993. return fn.next();
  7994. };
  7995. fn.next = function() {
  7996. return (index < size - 1) ? makeCallback(index + 1) : null;
  7997. };
  7998. return fn;
  7999. }
  8000. }
  8001. /**
  8002. * @memberof async
  8003. * @namespace apply
  8004. */
  8005. function apply(func) {
  8006. switch (arguments.length) {
  8007. case 0:
  8008. case 1:
  8009. return func;
  8010. case 2:
  8011. return func.bind(null, arguments[1]);
  8012. case 3:
  8013. return func.bind(null, arguments[1], arguments[2]);
  8014. case 4:
  8015. return func.bind(null, arguments[1], arguments[2], arguments[3]);
  8016. case 5:
  8017. return func.bind(null, arguments[1], arguments[2], arguments[3], arguments[4]);
  8018. default:
  8019. var size = arguments.length;
  8020. var index = 0;
  8021. var args = Array(size);
  8022. args[index] = null;
  8023. while (++index < size) {
  8024. args[index] = arguments[index];
  8025. }
  8026. return func.bind.apply(func, args);
  8027. }
  8028. }
  8029. /**
  8030. * @memberof async
  8031. * @namespace timeout
  8032. * @param {Function} func
  8033. * @param {number} millisec
  8034. * @param {*} info
  8035. */
  8036. function timeout(func, millisec, info) {
  8037. var callback, timer;
  8038. return wrappedFunc;
  8039. function wrappedFunc() {
  8040. timer = setTimeout(timeoutCallback, millisec);
  8041. var args = createArray(arguments);
  8042. var lastIndex = args.length - 1;
  8043. callback = args[lastIndex];
  8044. args[lastIndex] = injectedCallback;
  8045. simpleApply(func, args);
  8046. }
  8047. function timeoutCallback() {
  8048. var name = func.name || 'anonymous';
  8049. var err = new Error('Callback function "' + name + '" timed out.');
  8050. err.code = 'ETIMEDOUT';
  8051. if (info) {
  8052. err.info = info;
  8053. }
  8054. timer = null;
  8055. callback(err);
  8056. }
  8057. function injectedCallback() {
  8058. if (timer !== null) {
  8059. simpleApply(callback, createArray(arguments));
  8060. clearTimeout(timer);
  8061. }
  8062. }
  8063. function simpleApply(func, args) {
  8064. switch(args.length) {
  8065. case 0:
  8066. func();
  8067. break;
  8068. case 1:
  8069. func(args[0]);
  8070. break;
  8071. case 2:
  8072. func(args[0], args[1]);
  8073. break;
  8074. default:
  8075. func.apply(null, args);
  8076. break;
  8077. }
  8078. }
  8079. }
  8080. /**
  8081. * @memberof async
  8082. * @namespace times
  8083. * @param {number} n - n >= 1
  8084. * @param {Function} iterator
  8085. * @param {Function} callback
  8086. * @example
  8087. *
  8088. * var iterator = function(n, done) {
  8089. * done(null, n);
  8090. * };
  8091. * async.times(4, iterator, function(err, res) {
  8092. * console.log(res); // [0, 1, 2, 3];
  8093. * });
  8094. *
  8095. */
  8096. function times(n, iterator, callback) {
  8097. callback = callback || noop;
  8098. n = +n;
  8099. if (isNaN(n) || n < 1) {
  8100. return callback(null, []);
  8101. }
  8102. var result = Array(n);
  8103. timesSync(n, iterate);
  8104. function iterate(num) {
  8105. iterator(num, createCallback(num));
  8106. }
  8107. function createCallback(index) {
  8108. return function(err, res) {
  8109. if (index === null) {
  8110. throwError();
  8111. }
  8112. result[index] = res;
  8113. index = null;
  8114. if (err) {
  8115. callback(err);
  8116. callback = noop;
  8117. } else if (--n === 0) {
  8118. callback(null, result);
  8119. }
  8120. };
  8121. }
  8122. }
  8123. /**
  8124. * @memberof async
  8125. * @namespace timesSeries
  8126. * @param {number} n - n >= 1
  8127. * @param {Function} iterator
  8128. * @param {Function} callback
  8129. * @example
  8130. *
  8131. * var iterator = function(n, done) {
  8132. * done(null, n);
  8133. * };
  8134. * async.timesSeries(4, iterator, function(err, res) {
  8135. * console.log(res); // [0, 1, 2, 3];
  8136. * });
  8137. *
  8138. */
  8139. function timesSeries(n, iterator, callback) {
  8140. callback = callback || noop;
  8141. n = +n;
  8142. if (isNaN(n) || n < 1) {
  8143. return callback(null, []);
  8144. }
  8145. var result = Array(n);
  8146. var sync = false;
  8147. var completed = 0;
  8148. iterate();
  8149. function iterate() {
  8150. iterator(completed, done);
  8151. }
  8152. function done(err, res) {
  8153. result[completed] = res;
  8154. if (err) {
  8155. callback(err);
  8156. callback = throwError;
  8157. } else if (++completed >= n) {
  8158. callback(null, result);
  8159. callback = throwError;
  8160. } else if (sync) {
  8161. nextTick(iterate);
  8162. } else {
  8163. sync = true;
  8164. iterate();
  8165. }
  8166. sync = false;
  8167. }
  8168. }
  8169. /**
  8170. * @memberof async
  8171. * @namespace timesLimit
  8172. * @param {number} n - n >= 1
  8173. * @param {number} limit - n >= 1
  8174. * @param {Function} iterator
  8175. * @param {Function} callback
  8176. * @example
  8177. *
  8178. * var iterator = function(n, done) {
  8179. * done(null, n);
  8180. * };
  8181. * async.timesLimit(4, 2, iterator, function(err, res) {
  8182. * console.log(res); // [0, 1, 2, 3];
  8183. * });
  8184. *
  8185. */
  8186. function timesLimit(n, limit, iterator, callback) {
  8187. callback = callback || noop;
  8188. n = +n;
  8189. if (isNaN(n) || n < 1 || isNaN(limit) || limit < 1) {
  8190. return callback(null, []);
  8191. }
  8192. var result = Array(n);
  8193. var sync = false;
  8194. var started = 0;
  8195. var completed = 0;
  8196. timesSync(limit > n ? n : limit, iterate);
  8197. function iterate() {
  8198. var index = started++;
  8199. if (index < n) {
  8200. iterator(index, createCallback(index));
  8201. }
  8202. }
  8203. function createCallback(index) {
  8204. return function(err, res) {
  8205. if (index === null) {
  8206. throwError();
  8207. }
  8208. result[index] = res;
  8209. index = null;
  8210. if (err) {
  8211. callback(err);
  8212. callback = noop;
  8213. } else if (++completed >= n) {
  8214. callback(null, result);
  8215. callback = throwError;
  8216. } else if (sync) {
  8217. nextTick(iterate);
  8218. } else {
  8219. sync = true;
  8220. iterate();
  8221. }
  8222. sync = false;
  8223. };
  8224. }
  8225. }
  8226. /**
  8227. * @memberof async
  8228. * @namespace race
  8229. * @param {Array|Object} tasks - functions
  8230. * @param {Function} callback
  8231. * @example
  8232. *
  8233. * // array
  8234. * var called = 0;
  8235. * var tasks = [
  8236. * function(done) {
  8237. * setTimeout(function() {
  8238. * called++;
  8239. * done(null, '1');
  8240. * }, 30);
  8241. * },
  8242. * function(done) {
  8243. * setTimeout(function() {
  8244. * called++;
  8245. * done(null, '2');
  8246. * }, 20);
  8247. * },
  8248. * function(done) {
  8249. * setTimeout(function() {
  8250. * called++;
  8251. * done(null, '3');
  8252. * }, 10);
  8253. * }
  8254. * ];
  8255. * async.race(tasks, function(err, res) {
  8256. * console.log(res); // '3'
  8257. * console.log(called); // 1
  8258. * setTimeout(function() {
  8259. * console.log(called); // 3
  8260. * }, 50);
  8261. * });
  8262. *
  8263. * @example
  8264. *
  8265. * // object
  8266. * var called = 0;
  8267. * var tasks = {
  8268. * 'test1': function(done) {
  8269. * setTimeout(function() {
  8270. * called++;
  8271. * done(null, '1');
  8272. * }, 30);
  8273. * },
  8274. * 'test2': function(done) {
  8275. * setTimeout(function() {
  8276. * called++;
  8277. * done(null, '2');
  8278. * }, 20);
  8279. * },
  8280. * 'test3': function(done) {
  8281. * setTimeout(function() {
  8282. * called++;
  8283. * done(null, '3');
  8284. * }, 10);
  8285. * }
  8286. * };
  8287. * async.race(tasks, function(err, res) {
  8288. * console.log(res); // '3'
  8289. * console.log(called); // 1
  8290. * setTimeout(function() {
  8291. * console.log(called); // 3
  8292. * done();
  8293. * }, 50);
  8294. * });
  8295. *
  8296. */
  8297. function race(tasks, callback) {
  8298. callback = once(callback || noop);
  8299. var size, keys;
  8300. var index = -1;
  8301. if (isArray(tasks)) {
  8302. size = tasks.length;
  8303. while (++index < size) {
  8304. tasks[index](callback);
  8305. }
  8306. } else if (tasks && typeof tasks === obj) {
  8307. keys = nativeKeys(tasks);
  8308. size = keys.length;
  8309. while (++index < size) {
  8310. tasks[keys[index]](callback);
  8311. }
  8312. } else {
  8313. return callback(new TypeError('First argument to race must be a collection of functions'));
  8314. }
  8315. if (!size) {
  8316. callback(null);
  8317. }
  8318. }
  8319. /**
  8320. * @memberof async
  8321. * @namespace memoize
  8322. */
  8323. function memoize(fn, hasher) {
  8324. hasher = hasher || function(hash) {
  8325. return hash;
  8326. };
  8327. var memo = {};
  8328. var queues = {};
  8329. var memoized = function() {
  8330. var args = createArray(arguments);
  8331. var callback = args.pop();
  8332. var key = hasher.apply(null, args);
  8333. if (has(memo, key)) {
  8334. nextTick(function() {
  8335. callback.apply(null, memo[key]);
  8336. });
  8337. return;
  8338. }
  8339. if (has(queues, key)) {
  8340. return queues[key].push(callback);
  8341. }
  8342. queues[key] = [callback];
  8343. args.push(done);
  8344. fn.apply(null, args);
  8345. function done() {
  8346. var args = createArray(arguments);
  8347. memo[key] = args;
  8348. var q = queues[key];
  8349. delete queues[key];
  8350. var i = -1;
  8351. var size = q.length;
  8352. while (++i < size) {
  8353. q[i].apply(null, args);
  8354. }
  8355. }
  8356. };
  8357. memoized.memo = memo;
  8358. memoized.unmemoized = fn;
  8359. return memoized;
  8360. }
  8361. /**
  8362. * @memberof async
  8363. * @namespace unmemoize
  8364. */
  8365. function unmemoize(fn) {
  8366. return function() {
  8367. return (fn.unmemoized || fn).apply(null, arguments);
  8368. };
  8369. }
  8370. /**
  8371. * @memberof async
  8372. * @namespace ensureAsync
  8373. */
  8374. function ensureAsync(fn) {
  8375. return function( /* ...args, callback */ ) {
  8376. var args = createArray(arguments);
  8377. var lastIndex = args.length - 1;
  8378. var callback = args[lastIndex];
  8379. var sync = true;
  8380. args[lastIndex] = done;
  8381. fn.apply(this, args);
  8382. sync = false;
  8383. function done() {
  8384. var innerArgs = createArray(arguments);
  8385. if (sync) {
  8386. nextTick(function() {
  8387. callback.apply(null, innerArgs);
  8388. });
  8389. } else {
  8390. callback.apply(null, innerArgs);
  8391. }
  8392. }
  8393. };
  8394. }
  8395. /**
  8396. * @memberof async
  8397. * @namespace constant
  8398. */
  8399. function constant( /* values... */ ) {
  8400. var args = [null].concat(createArray(arguments));
  8401. return function(callback) {
  8402. callback = arguments[arguments.length - 1];
  8403. callback.apply(this, args);
  8404. };
  8405. }
  8406. function asyncify(fn) {
  8407. return function( /* args..., callback */ ) {
  8408. var args = createArray(arguments);
  8409. var callback = args.pop();
  8410. var result;
  8411. try {
  8412. result = fn.apply(this, args);
  8413. } catch (e) {
  8414. return callback(e);
  8415. }
  8416. if (result && typeof result.then === func) {
  8417. result.then(function(value) {
  8418. invokeCallback(callback, null, value);
  8419. }, function(err) {
  8420. invokeCallback(callback, err && err.message ? err : new Error(err));
  8421. });
  8422. } else {
  8423. callback(null, result);
  8424. }
  8425. };
  8426. }
  8427. function invokeCallback(callback, err, value) {
  8428. try {
  8429. callback(err, value);
  8430. } catch (e) {
  8431. nextTick(rethrow, e);
  8432. }
  8433. }
  8434. function rethrow(error) {
  8435. throw error;
  8436. }
  8437. /**
  8438. * @memberof async
  8439. * @namespace reflect
  8440. * @param {Function} func
  8441. * @return {Function}
  8442. */
  8443. function reflect(func) {
  8444. return function( /* args..., callback */ ) {
  8445. var callback;
  8446. switch (arguments.length) {
  8447. case 1:
  8448. callback = arguments[0];
  8449. return func(done);
  8450. case 2:
  8451. callback = arguments[1];
  8452. return func(arguments[0], done);
  8453. default:
  8454. var args = createArray(arguments);
  8455. var lastIndex = args.length - 1;
  8456. callback = args[lastIndex];
  8457. args[lastIndex] = done;
  8458. func.apply(this, args);
  8459. }
  8460. function done(err, res) {
  8461. if (err) {
  8462. return callback(null, {
  8463. error: err
  8464. });
  8465. }
  8466. if (arguments.length > 2) {
  8467. res = slice(arguments, 1);
  8468. }
  8469. callback(null, {
  8470. value: res
  8471. });
  8472. }
  8473. };
  8474. }
  8475. /**
  8476. * @memberof async
  8477. * @namespace reflectAll
  8478. * @param {Array[]|Object} tasks
  8479. * @return {Function}
  8480. */
  8481. function reflectAll(tasks) {
  8482. var size, newTasks, keys;
  8483. if (isArray(tasks)) {
  8484. size = tasks.length;
  8485. newTasks = Array(size);
  8486. arrayEachSync(tasks, iterate);
  8487. } else if (tasks && typeof tasks === obj) {
  8488. keys = nativeKeys(tasks);
  8489. size = keys.length;
  8490. newTasks = {};
  8491. baseEachSync(tasks, iterate, keys);
  8492. }
  8493. return newTasks;
  8494. function iterate(func, key) {
  8495. newTasks[key] = reflect(func);
  8496. }
  8497. }
  8498. /**
  8499. * @memberof async
  8500. * @namespace createLogger
  8501. */
  8502. function createLogger(name) {
  8503. return function(fn) {
  8504. var args = slice(arguments, 1);
  8505. args.push(done);
  8506. fn.apply(null, args);
  8507. };
  8508. function done(err) {
  8509. if (typeof console === obj) {
  8510. if (err) {
  8511. if (console.error) {
  8512. console.error(err);
  8513. }
  8514. return;
  8515. }
  8516. if (console[name]) {
  8517. var args = slice(arguments, 1);
  8518. arrayEachSync(args, function(arg) {
  8519. console[name](arg);
  8520. });
  8521. }
  8522. }
  8523. }
  8524. }
  8525. /**
  8526. * @memberof async
  8527. * @namespace safe
  8528. */
  8529. function safe() {
  8530. createImmediate();
  8531. return exports;
  8532. }
  8533. /**
  8534. * @memberof async
  8535. * @namespace fast
  8536. */
  8537. function fast() {
  8538. createImmediate(false);
  8539. return exports;
  8540. }
  8541. }));