async.js 225 KB

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