async.js 230 KB

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