Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

async.js 230KB

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