Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bluebird.js 174KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623
  1. /* @preserve
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2013-2017 Petka Antonov
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. /**
  26. * bluebird build version 3.5.1
  27. * Features enabled: core, race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each
  28. */
  29. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Promise=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof _dereq_=="function"&&_dereq_;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof _dereq_=="function"&&_dereq_;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  30. "use strict";
  31. module.exports = function(Promise) {
  32. var SomePromiseArray = Promise._SomePromiseArray;
  33. function any(promises) {
  34. var ret = new SomePromiseArray(promises);
  35. var promise = ret.promise();
  36. ret.setHowMany(1);
  37. ret.setUnwrap();
  38. ret.init();
  39. return promise;
  40. }
  41. Promise.any = function (promises) {
  42. return any(promises);
  43. };
  44. Promise.prototype.any = function () {
  45. return any(this);
  46. };
  47. };
  48. },{}],2:[function(_dereq_,module,exports){
  49. "use strict";
  50. var firstLineError;
  51. try {throw new Error(); } catch (e) {firstLineError = e;}
  52. var schedule = _dereq_("./schedule");
  53. var Queue = _dereq_("./queue");
  54. var util = _dereq_("./util");
  55. function Async() {
  56. this._customScheduler = false;
  57. this._isTickUsed = false;
  58. this._lateQueue = new Queue(16);
  59. this._normalQueue = new Queue(16);
  60. this._haveDrainedQueues = false;
  61. this._trampolineEnabled = true;
  62. var self = this;
  63. this.drainQueues = function () {
  64. self._drainQueues();
  65. };
  66. this._schedule = schedule;
  67. }
  68. Async.prototype.setScheduler = function(fn) {
  69. var prev = this._schedule;
  70. this._schedule = fn;
  71. this._customScheduler = true;
  72. return prev;
  73. };
  74. Async.prototype.hasCustomScheduler = function() {
  75. return this._customScheduler;
  76. };
  77. Async.prototype.enableTrampoline = function() {
  78. this._trampolineEnabled = true;
  79. };
  80. Async.prototype.disableTrampolineIfNecessary = function() {
  81. if (util.hasDevTools) {
  82. this._trampolineEnabled = false;
  83. }
  84. };
  85. Async.prototype.haveItemsQueued = function () {
  86. return this._isTickUsed || this._haveDrainedQueues;
  87. };
  88. Async.prototype.fatalError = function(e, isNode) {
  89. if (isNode) {
  90. process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e) +
  91. "\n");
  92. process.exit(2);
  93. } else {
  94. this.throwLater(e);
  95. }
  96. };
  97. Async.prototype.throwLater = function(fn, arg) {
  98. if (arguments.length === 1) {
  99. arg = fn;
  100. fn = function () { throw arg; };
  101. }
  102. if (typeof setTimeout !== "undefined") {
  103. setTimeout(function() {
  104. fn(arg);
  105. }, 0);
  106. } else try {
  107. this._schedule(function() {
  108. fn(arg);
  109. });
  110. } catch (e) {
  111. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  112. }
  113. };
  114. function AsyncInvokeLater(fn, receiver, arg) {
  115. this._lateQueue.push(fn, receiver, arg);
  116. this._queueTick();
  117. }
  118. function AsyncInvoke(fn, receiver, arg) {
  119. this._normalQueue.push(fn, receiver, arg);
  120. this._queueTick();
  121. }
  122. function AsyncSettlePromises(promise) {
  123. this._normalQueue._pushOne(promise);
  124. this._queueTick();
  125. }
  126. if (!util.hasDevTools) {
  127. Async.prototype.invokeLater = AsyncInvokeLater;
  128. Async.prototype.invoke = AsyncInvoke;
  129. Async.prototype.settlePromises = AsyncSettlePromises;
  130. } else {
  131. Async.prototype.invokeLater = function (fn, receiver, arg) {
  132. if (this._trampolineEnabled) {
  133. AsyncInvokeLater.call(this, fn, receiver, arg);
  134. } else {
  135. this._schedule(function() {
  136. setTimeout(function() {
  137. fn.call(receiver, arg);
  138. }, 100);
  139. });
  140. }
  141. };
  142. Async.prototype.invoke = function (fn, receiver, arg) {
  143. if (this._trampolineEnabled) {
  144. AsyncInvoke.call(this, fn, receiver, arg);
  145. } else {
  146. this._schedule(function() {
  147. fn.call(receiver, arg);
  148. });
  149. }
  150. };
  151. Async.prototype.settlePromises = function(promise) {
  152. if (this._trampolineEnabled) {
  153. AsyncSettlePromises.call(this, promise);
  154. } else {
  155. this._schedule(function() {
  156. promise._settlePromises();
  157. });
  158. }
  159. };
  160. }
  161. Async.prototype._drainQueue = function(queue) {
  162. while (queue.length() > 0) {
  163. var fn = queue.shift();
  164. if (typeof fn !== "function") {
  165. fn._settlePromises();
  166. continue;
  167. }
  168. var receiver = queue.shift();
  169. var arg = queue.shift();
  170. fn.call(receiver, arg);
  171. }
  172. };
  173. Async.prototype._drainQueues = function () {
  174. this._drainQueue(this._normalQueue);
  175. this._reset();
  176. this._haveDrainedQueues = true;
  177. this._drainQueue(this._lateQueue);
  178. };
  179. Async.prototype._queueTick = function () {
  180. if (!this._isTickUsed) {
  181. this._isTickUsed = true;
  182. this._schedule(this.drainQueues);
  183. }
  184. };
  185. Async.prototype._reset = function () {
  186. this._isTickUsed = false;
  187. };
  188. module.exports = Async;
  189. module.exports.firstLineError = firstLineError;
  190. },{"./queue":26,"./schedule":29,"./util":36}],3:[function(_dereq_,module,exports){
  191. "use strict";
  192. module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
  193. var calledBind = false;
  194. var rejectThis = function(_, e) {
  195. this._reject(e);
  196. };
  197. var targetRejected = function(e, context) {
  198. context.promiseRejectionQueued = true;
  199. context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
  200. };
  201. var bindingResolved = function(thisArg, context) {
  202. if (((this._bitField & 50397184) === 0)) {
  203. this._resolveCallback(context.target);
  204. }
  205. };
  206. var bindingRejected = function(e, context) {
  207. if (!context.promiseRejectionQueued) this._reject(e);
  208. };
  209. Promise.prototype.bind = function (thisArg) {
  210. if (!calledBind) {
  211. calledBind = true;
  212. Promise.prototype._propagateFrom = debug.propagateFromFunction();
  213. Promise.prototype._boundValue = debug.boundValueFunction();
  214. }
  215. var maybePromise = tryConvertToPromise(thisArg);
  216. var ret = new Promise(INTERNAL);
  217. ret._propagateFrom(this, 1);
  218. var target = this._target();
  219. ret._setBoundTo(maybePromise);
  220. if (maybePromise instanceof Promise) {
  221. var context = {
  222. promiseRejectionQueued: false,
  223. promise: ret,
  224. target: target,
  225. bindingPromise: maybePromise
  226. };
  227. target._then(INTERNAL, targetRejected, undefined, ret, context);
  228. maybePromise._then(
  229. bindingResolved, bindingRejected, undefined, ret, context);
  230. ret._setOnCancel(maybePromise);
  231. } else {
  232. ret._resolveCallback(target);
  233. }
  234. return ret;
  235. };
  236. Promise.prototype._setBoundTo = function (obj) {
  237. if (obj !== undefined) {
  238. this._bitField = this._bitField | 2097152;
  239. this._boundTo = obj;
  240. } else {
  241. this._bitField = this._bitField & (~2097152);
  242. }
  243. };
  244. Promise.prototype._isBound = function () {
  245. return (this._bitField & 2097152) === 2097152;
  246. };
  247. Promise.bind = function (thisArg, value) {
  248. return Promise.resolve(value).bind(thisArg);
  249. };
  250. };
  251. },{}],4:[function(_dereq_,module,exports){
  252. "use strict";
  253. var old;
  254. if (typeof Promise !== "undefined") old = Promise;
  255. function noConflict() {
  256. try { if (Promise === bluebird) Promise = old; }
  257. catch (e) {}
  258. return bluebird;
  259. }
  260. var bluebird = _dereq_("./promise")();
  261. bluebird.noConflict = noConflict;
  262. module.exports = bluebird;
  263. },{"./promise":22}],5:[function(_dereq_,module,exports){
  264. "use strict";
  265. var cr = Object.create;
  266. if (cr) {
  267. var callerCache = cr(null);
  268. var getterCache = cr(null);
  269. callerCache[" size"] = getterCache[" size"] = 0;
  270. }
  271. module.exports = function(Promise) {
  272. var util = _dereq_("./util");
  273. var canEvaluate = util.canEvaluate;
  274. var isIdentifier = util.isIdentifier;
  275. var getMethodCaller;
  276. var getGetter;
  277. if (!true) {
  278. var makeMethodCaller = function (methodName) {
  279. return new Function("ensureMethod", " \n\
  280. return function(obj) { \n\
  281. 'use strict' \n\
  282. var len = this.length; \n\
  283. ensureMethod(obj, 'methodName'); \n\
  284. switch(len) { \n\
  285. case 1: return obj.methodName(this[0]); \n\
  286. case 2: return obj.methodName(this[0], this[1]); \n\
  287. case 3: return obj.methodName(this[0], this[1], this[2]); \n\
  288. case 0: return obj.methodName(); \n\
  289. default: \n\
  290. return obj.methodName.apply(obj, this); \n\
  291. } \n\
  292. }; \n\
  293. ".replace(/methodName/g, methodName))(ensureMethod);
  294. };
  295. var makeGetter = function (propertyName) {
  296. return new Function("obj", " \n\
  297. 'use strict'; \n\
  298. return obj.propertyName; \n\
  299. ".replace("propertyName", propertyName));
  300. };
  301. var getCompiled = function(name, compiler, cache) {
  302. var ret = cache[name];
  303. if (typeof ret !== "function") {
  304. if (!isIdentifier(name)) {
  305. return null;
  306. }
  307. ret = compiler(name);
  308. cache[name] = ret;
  309. cache[" size"]++;
  310. if (cache[" size"] > 512) {
  311. var keys = Object.keys(cache);
  312. for (var i = 0; i < 256; ++i) delete cache[keys[i]];
  313. cache[" size"] = keys.length - 256;
  314. }
  315. }
  316. return ret;
  317. };
  318. getMethodCaller = function(name) {
  319. return getCompiled(name, makeMethodCaller, callerCache);
  320. };
  321. getGetter = function(name) {
  322. return getCompiled(name, makeGetter, getterCache);
  323. };
  324. }
  325. function ensureMethod(obj, methodName) {
  326. var fn;
  327. if (obj != null) fn = obj[methodName];
  328. if (typeof fn !== "function") {
  329. var message = "Object " + util.classString(obj) + " has no method '" +
  330. util.toString(methodName) + "'";
  331. throw new Promise.TypeError(message);
  332. }
  333. return fn;
  334. }
  335. function caller(obj) {
  336. var methodName = this.pop();
  337. var fn = ensureMethod(obj, methodName);
  338. return fn.apply(obj, this);
  339. }
  340. Promise.prototype.call = function (methodName) {
  341. var args = [].slice.call(arguments, 1);;
  342. if (!true) {
  343. if (canEvaluate) {
  344. var maybeCaller = getMethodCaller(methodName);
  345. if (maybeCaller !== null) {
  346. return this._then(
  347. maybeCaller, undefined, undefined, args, undefined);
  348. }
  349. }
  350. }
  351. args.push(methodName);
  352. return this._then(caller, undefined, undefined, args, undefined);
  353. };
  354. function namedGetter(obj) {
  355. return obj[this];
  356. }
  357. function indexedGetter(obj) {
  358. var index = +this;
  359. if (index < 0) index = Math.max(0, index + obj.length);
  360. return obj[index];
  361. }
  362. Promise.prototype.get = function (propertyName) {
  363. var isIndex = (typeof propertyName === "number");
  364. var getter;
  365. if (!isIndex) {
  366. if (canEvaluate) {
  367. var maybeGetter = getGetter(propertyName);
  368. getter = maybeGetter !== null ? maybeGetter : namedGetter;
  369. } else {
  370. getter = namedGetter;
  371. }
  372. } else {
  373. getter = indexedGetter;
  374. }
  375. return this._then(getter, undefined, undefined, propertyName, undefined);
  376. };
  377. };
  378. },{"./util":36}],6:[function(_dereq_,module,exports){
  379. "use strict";
  380. module.exports = function(Promise, PromiseArray, apiRejection, debug) {
  381. var util = _dereq_("./util");
  382. var tryCatch = util.tryCatch;
  383. var errorObj = util.errorObj;
  384. var async = Promise._async;
  385. Promise.prototype["break"] = Promise.prototype.cancel = function() {
  386. if (!debug.cancellation()) return this._warn("cancellation is disabled");
  387. var promise = this;
  388. var child = promise;
  389. while (promise._isCancellable()) {
  390. if (!promise._cancelBy(child)) {
  391. if (child._isFollowing()) {
  392. child._followee().cancel();
  393. } else {
  394. child._cancelBranched();
  395. }
  396. break;
  397. }
  398. var parent = promise._cancellationParent;
  399. if (parent == null || !parent._isCancellable()) {
  400. if (promise._isFollowing()) {
  401. promise._followee().cancel();
  402. } else {
  403. promise._cancelBranched();
  404. }
  405. break;
  406. } else {
  407. if (promise._isFollowing()) promise._followee().cancel();
  408. promise._setWillBeCancelled();
  409. child = promise;
  410. promise = parent;
  411. }
  412. }
  413. };
  414. Promise.prototype._branchHasCancelled = function() {
  415. this._branchesRemainingToCancel--;
  416. };
  417. Promise.prototype._enoughBranchesHaveCancelled = function() {
  418. return this._branchesRemainingToCancel === undefined ||
  419. this._branchesRemainingToCancel <= 0;
  420. };
  421. Promise.prototype._cancelBy = function(canceller) {
  422. if (canceller === this) {
  423. this._branchesRemainingToCancel = 0;
  424. this._invokeOnCancel();
  425. return true;
  426. } else {
  427. this._branchHasCancelled();
  428. if (this._enoughBranchesHaveCancelled()) {
  429. this._invokeOnCancel();
  430. return true;
  431. }
  432. }
  433. return false;
  434. };
  435. Promise.prototype._cancelBranched = function() {
  436. if (this._enoughBranchesHaveCancelled()) {
  437. this._cancel();
  438. }
  439. };
  440. Promise.prototype._cancel = function() {
  441. if (!this._isCancellable()) return;
  442. this._setCancelled();
  443. async.invoke(this._cancelPromises, this, undefined);
  444. };
  445. Promise.prototype._cancelPromises = function() {
  446. if (this._length() > 0) this._settlePromises();
  447. };
  448. Promise.prototype._unsetOnCancel = function() {
  449. this._onCancelField = undefined;
  450. };
  451. Promise.prototype._isCancellable = function() {
  452. return this.isPending() && !this._isCancelled();
  453. };
  454. Promise.prototype.isCancellable = function() {
  455. return this.isPending() && !this.isCancelled();
  456. };
  457. Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) {
  458. if (util.isArray(onCancelCallback)) {
  459. for (var i = 0; i < onCancelCallback.length; ++i) {
  460. this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
  461. }
  462. } else if (onCancelCallback !== undefined) {
  463. if (typeof onCancelCallback === "function") {
  464. if (!internalOnly) {
  465. var e = tryCatch(onCancelCallback).call(this._boundValue());
  466. if (e === errorObj) {
  467. this._attachExtraTrace(e.e);
  468. async.throwLater(e.e);
  469. }
  470. }
  471. } else {
  472. onCancelCallback._resultCancelled(this);
  473. }
  474. }
  475. };
  476. Promise.prototype._invokeOnCancel = function() {
  477. var onCancelCallback = this._onCancel();
  478. this._unsetOnCancel();
  479. async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
  480. };
  481. Promise.prototype._invokeInternalOnCancel = function() {
  482. if (this._isCancellable()) {
  483. this._doInvokeOnCancel(this._onCancel(), true);
  484. this._unsetOnCancel();
  485. }
  486. };
  487. Promise.prototype._resultCancelled = function() {
  488. this.cancel();
  489. };
  490. };
  491. },{"./util":36}],7:[function(_dereq_,module,exports){
  492. "use strict";
  493. module.exports = function(NEXT_FILTER) {
  494. var util = _dereq_("./util");
  495. var getKeys = _dereq_("./es5").keys;
  496. var tryCatch = util.tryCatch;
  497. var errorObj = util.errorObj;
  498. function catchFilter(instances, cb, promise) {
  499. return function(e) {
  500. var boundTo = promise._boundValue();
  501. predicateLoop: for (var i = 0; i < instances.length; ++i) {
  502. var item = instances[i];
  503. if (item === Error ||
  504. (item != null && item.prototype instanceof Error)) {
  505. if (e instanceof item) {
  506. return tryCatch(cb).call(boundTo, e);
  507. }
  508. } else if (typeof item === "function") {
  509. var matchesPredicate = tryCatch(item).call(boundTo, e);
  510. if (matchesPredicate === errorObj) {
  511. return matchesPredicate;
  512. } else if (matchesPredicate) {
  513. return tryCatch(cb).call(boundTo, e);
  514. }
  515. } else if (util.isObject(e)) {
  516. var keys = getKeys(item);
  517. for (var j = 0; j < keys.length; ++j) {
  518. var key = keys[j];
  519. if (item[key] != e[key]) {
  520. continue predicateLoop;
  521. }
  522. }
  523. return tryCatch(cb).call(boundTo, e);
  524. }
  525. }
  526. return NEXT_FILTER;
  527. };
  528. }
  529. return catchFilter;
  530. };
  531. },{"./es5":13,"./util":36}],8:[function(_dereq_,module,exports){
  532. "use strict";
  533. module.exports = function(Promise) {
  534. var longStackTraces = false;
  535. var contextStack = [];
  536. Promise.prototype._promiseCreated = function() {};
  537. Promise.prototype._pushContext = function() {};
  538. Promise.prototype._popContext = function() {return null;};
  539. Promise._peekContext = Promise.prototype._peekContext = function() {};
  540. function Context() {
  541. this._trace = new Context.CapturedTrace(peekContext());
  542. }
  543. Context.prototype._pushContext = function () {
  544. if (this._trace !== undefined) {
  545. this._trace._promiseCreated = null;
  546. contextStack.push(this._trace);
  547. }
  548. };
  549. Context.prototype._popContext = function () {
  550. if (this._trace !== undefined) {
  551. var trace = contextStack.pop();
  552. var ret = trace._promiseCreated;
  553. trace._promiseCreated = null;
  554. return ret;
  555. }
  556. return null;
  557. };
  558. function createContext() {
  559. if (longStackTraces) return new Context();
  560. }
  561. function peekContext() {
  562. var lastIndex = contextStack.length - 1;
  563. if (lastIndex >= 0) {
  564. return contextStack[lastIndex];
  565. }
  566. return undefined;
  567. }
  568. Context.CapturedTrace = null;
  569. Context.create = createContext;
  570. Context.deactivateLongStackTraces = function() {};
  571. Context.activateLongStackTraces = function() {
  572. var Promise_pushContext = Promise.prototype._pushContext;
  573. var Promise_popContext = Promise.prototype._popContext;
  574. var Promise_PeekContext = Promise._peekContext;
  575. var Promise_peekContext = Promise.prototype._peekContext;
  576. var Promise_promiseCreated = Promise.prototype._promiseCreated;
  577. Context.deactivateLongStackTraces = function() {
  578. Promise.prototype._pushContext = Promise_pushContext;
  579. Promise.prototype._popContext = Promise_popContext;
  580. Promise._peekContext = Promise_PeekContext;
  581. Promise.prototype._peekContext = Promise_peekContext;
  582. Promise.prototype._promiseCreated = Promise_promiseCreated;
  583. longStackTraces = false;
  584. };
  585. longStackTraces = true;
  586. Promise.prototype._pushContext = Context.prototype._pushContext;
  587. Promise.prototype._popContext = Context.prototype._popContext;
  588. Promise._peekContext = Promise.prototype._peekContext = peekContext;
  589. Promise.prototype._promiseCreated = function() {
  590. var ctx = this._peekContext();
  591. if (ctx && ctx._promiseCreated == null) ctx._promiseCreated = this;
  592. };
  593. };
  594. return Context;
  595. };
  596. },{}],9:[function(_dereq_,module,exports){
  597. "use strict";
  598. module.exports = function(Promise, Context) {
  599. var getDomain = Promise._getDomain;
  600. var async = Promise._async;
  601. var Warning = _dereq_("./errors").Warning;
  602. var util = _dereq_("./util");
  603. var canAttachTrace = util.canAttachTrace;
  604. var unhandledRejectionHandled;
  605. var possiblyUnhandledRejection;
  606. var bluebirdFramePattern =
  607. /[\\\/]bluebird[\\\/]js[\\\/](release|debug|instrumented)/;
  608. var nodeFramePattern = /\((?:timers\.js):\d+:\d+\)/;
  609. var parseLinePattern = /[\/<\(](.+?):(\d+):(\d+)\)?\s*$/;
  610. var stackFramePattern = null;
  611. var formatStack = null;
  612. var indentStackFrames = false;
  613. var printWarning;
  614. var debugging = !!(util.env("BLUEBIRD_DEBUG") != 0 &&
  615. (true ||
  616. util.env("BLUEBIRD_DEBUG") ||
  617. util.env("NODE_ENV") === "development"));
  618. var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 &&
  619. (debugging || util.env("BLUEBIRD_WARNINGS")));
  620. var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
  621. (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));
  622. var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
  623. (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
  624. Promise.prototype.suppressUnhandledRejections = function() {
  625. var target = this._target();
  626. target._bitField = ((target._bitField & (~1048576)) |
  627. 524288);
  628. };
  629. Promise.prototype._ensurePossibleRejectionHandled = function () {
  630. if ((this._bitField & 524288) !== 0) return;
  631. this._setRejectionIsUnhandled();
  632. var self = this;
  633. setTimeout(function() {
  634. self._notifyUnhandledRejection();
  635. }, 1);
  636. };
  637. Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
  638. fireRejectionEvent("rejectionHandled",
  639. unhandledRejectionHandled, undefined, this);
  640. };
  641. Promise.prototype._setReturnedNonUndefined = function() {
  642. this._bitField = this._bitField | 268435456;
  643. };
  644. Promise.prototype._returnedNonUndefined = function() {
  645. return (this._bitField & 268435456) !== 0;
  646. };
  647. Promise.prototype._notifyUnhandledRejection = function () {
  648. if (this._isRejectionUnhandled()) {
  649. var reason = this._settledValue();
  650. this._setUnhandledRejectionIsNotified();
  651. fireRejectionEvent("unhandledRejection",
  652. possiblyUnhandledRejection, reason, this);
  653. }
  654. };
  655. Promise.prototype._setUnhandledRejectionIsNotified = function () {
  656. this._bitField = this._bitField | 262144;
  657. };
  658. Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
  659. this._bitField = this._bitField & (~262144);
  660. };
  661. Promise.prototype._isUnhandledRejectionNotified = function () {
  662. return (this._bitField & 262144) > 0;
  663. };
  664. Promise.prototype._setRejectionIsUnhandled = function () {
  665. this._bitField = this._bitField | 1048576;
  666. };
  667. Promise.prototype._unsetRejectionIsUnhandled = function () {
  668. this._bitField = this._bitField & (~1048576);
  669. if (this._isUnhandledRejectionNotified()) {
  670. this._unsetUnhandledRejectionIsNotified();
  671. this._notifyUnhandledRejectionIsHandled();
  672. }
  673. };
  674. Promise.prototype._isRejectionUnhandled = function () {
  675. return (this._bitField & 1048576) > 0;
  676. };
  677. Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) {
  678. return warn(message, shouldUseOwnTrace, promise || this);
  679. };
  680. Promise.onPossiblyUnhandledRejection = function (fn) {
  681. var domain = getDomain();
  682. possiblyUnhandledRejection =
  683. typeof fn === "function" ? (domain === null ?
  684. fn : util.domainBind(domain, fn))
  685. : undefined;
  686. };
  687. Promise.onUnhandledRejectionHandled = function (fn) {
  688. var domain = getDomain();
  689. unhandledRejectionHandled =
  690. typeof fn === "function" ? (domain === null ?
  691. fn : util.domainBind(domain, fn))
  692. : undefined;
  693. };
  694. var disableLongStackTraces = function() {};
  695. Promise.longStackTraces = function () {
  696. if (async.haveItemsQueued() && !config.longStackTraces) {
  697. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  698. }
  699. if (!config.longStackTraces && longStackTracesIsSupported()) {
  700. var Promise_captureStackTrace = Promise.prototype._captureStackTrace;
  701. var Promise_attachExtraTrace = Promise.prototype._attachExtraTrace;
  702. config.longStackTraces = true;
  703. disableLongStackTraces = function() {
  704. if (async.haveItemsQueued() && !config.longStackTraces) {
  705. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  706. }
  707. Promise.prototype._captureStackTrace = Promise_captureStackTrace;
  708. Promise.prototype._attachExtraTrace = Promise_attachExtraTrace;
  709. Context.deactivateLongStackTraces();
  710. async.enableTrampoline();
  711. config.longStackTraces = false;
  712. };
  713. Promise.prototype._captureStackTrace = longStackTracesCaptureStackTrace;
  714. Promise.prototype._attachExtraTrace = longStackTracesAttachExtraTrace;
  715. Context.activateLongStackTraces();
  716. async.disableTrampolineIfNecessary();
  717. }
  718. };
  719. Promise.hasLongStackTraces = function () {
  720. return config.longStackTraces && longStackTracesIsSupported();
  721. };
  722. var fireDomEvent = (function() {
  723. try {
  724. if (typeof CustomEvent === "function") {
  725. var event = new CustomEvent("CustomEvent");
  726. util.global.dispatchEvent(event);
  727. return function(name, event) {
  728. var domEvent = new CustomEvent(name.toLowerCase(), {
  729. detail: event,
  730. cancelable: true
  731. });
  732. return !util.global.dispatchEvent(domEvent);
  733. };
  734. } else if (typeof Event === "function") {
  735. var event = new Event("CustomEvent");
  736. util.global.dispatchEvent(event);
  737. return function(name, event) {
  738. var domEvent = new Event(name.toLowerCase(), {
  739. cancelable: true
  740. });
  741. domEvent.detail = event;
  742. return !util.global.dispatchEvent(domEvent);
  743. };
  744. } else {
  745. var event = document.createEvent("CustomEvent");
  746. event.initCustomEvent("testingtheevent", false, true, {});
  747. util.global.dispatchEvent(event);
  748. return function(name, event) {
  749. var domEvent = document.createEvent("CustomEvent");
  750. domEvent.initCustomEvent(name.toLowerCase(), false, true,
  751. event);
  752. return !util.global.dispatchEvent(domEvent);
  753. };
  754. }
  755. } catch (e) {}
  756. return function() {
  757. return false;
  758. };
  759. })();
  760. var fireGlobalEvent = (function() {
  761. if (util.isNode) {
  762. return function() {
  763. return process.emit.apply(process, arguments);
  764. };
  765. } else {
  766. if (!util.global) {
  767. return function() {
  768. return false;
  769. };
  770. }
  771. return function(name) {
  772. var methodName = "on" + name.toLowerCase();
  773. var method = util.global[methodName];
  774. if (!method) return false;
  775. method.apply(util.global, [].slice.call(arguments, 1));
  776. return true;
  777. };
  778. }
  779. })();
  780. function generatePromiseLifecycleEventObject(name, promise) {
  781. return {promise: promise};
  782. }
  783. var eventToObjectGenerator = {
  784. promiseCreated: generatePromiseLifecycleEventObject,
  785. promiseFulfilled: generatePromiseLifecycleEventObject,
  786. promiseRejected: generatePromiseLifecycleEventObject,
  787. promiseResolved: generatePromiseLifecycleEventObject,
  788. promiseCancelled: generatePromiseLifecycleEventObject,
  789. promiseChained: function(name, promise, child) {
  790. return {promise: promise, child: child};
  791. },
  792. warning: function(name, warning) {
  793. return {warning: warning};
  794. },
  795. unhandledRejection: function (name, reason, promise) {
  796. return {reason: reason, promise: promise};
  797. },
  798. rejectionHandled: generatePromiseLifecycleEventObject
  799. };
  800. var activeFireEvent = function (name) {
  801. var globalEventFired = false;
  802. try {
  803. globalEventFired = fireGlobalEvent.apply(null, arguments);
  804. } catch (e) {
  805. async.throwLater(e);
  806. globalEventFired = true;
  807. }
  808. var domEventFired = false;
  809. try {
  810. domEventFired = fireDomEvent(name,
  811. eventToObjectGenerator[name].apply(null, arguments));
  812. } catch (e) {
  813. async.throwLater(e);
  814. domEventFired = true;
  815. }
  816. return domEventFired || globalEventFired;
  817. };
  818. Promise.config = function(opts) {
  819. opts = Object(opts);
  820. if ("longStackTraces" in opts) {
  821. if (opts.longStackTraces) {
  822. Promise.longStackTraces();
  823. } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {
  824. disableLongStackTraces();
  825. }
  826. }
  827. if ("warnings" in opts) {
  828. var warningsOption = opts.warnings;
  829. config.warnings = !!warningsOption;
  830. wForgottenReturn = config.warnings;
  831. if (util.isObject(warningsOption)) {
  832. if ("wForgottenReturn" in warningsOption) {
  833. wForgottenReturn = !!warningsOption.wForgottenReturn;
  834. }
  835. }
  836. }
  837. if ("cancellation" in opts && opts.cancellation && !config.cancellation) {
  838. if (async.haveItemsQueued()) {
  839. throw new Error(
  840. "cannot enable cancellation after promises are in use");
  841. }
  842. Promise.prototype._clearCancellationData =
  843. cancellationClearCancellationData;
  844. Promise.prototype._propagateFrom = cancellationPropagateFrom;
  845. Promise.prototype._onCancel = cancellationOnCancel;
  846. Promise.prototype._setOnCancel = cancellationSetOnCancel;
  847. Promise.prototype._attachCancellationCallback =
  848. cancellationAttachCancellationCallback;
  849. Promise.prototype._execute = cancellationExecute;
  850. propagateFromFunction = cancellationPropagateFrom;
  851. config.cancellation = true;
  852. }
  853. if ("monitoring" in opts) {
  854. if (opts.monitoring && !config.monitoring) {
  855. config.monitoring = true;
  856. Promise.prototype._fireEvent = activeFireEvent;
  857. } else if (!opts.monitoring && config.monitoring) {
  858. config.monitoring = false;
  859. Promise.prototype._fireEvent = defaultFireEvent;
  860. }
  861. }
  862. return Promise;
  863. };
  864. function defaultFireEvent() { return false; }
  865. Promise.prototype._fireEvent = defaultFireEvent;
  866. Promise.prototype._execute = function(executor, resolve, reject) {
  867. try {
  868. executor(resolve, reject);
  869. } catch (e) {
  870. return e;
  871. }
  872. };
  873. Promise.prototype._onCancel = function () {};
  874. Promise.prototype._setOnCancel = function (handler) { ; };
  875. Promise.prototype._attachCancellationCallback = function(onCancel) {
  876. ;
  877. };
  878. Promise.prototype._captureStackTrace = function () {};
  879. Promise.prototype._attachExtraTrace = function () {};
  880. Promise.prototype._clearCancellationData = function() {};
  881. Promise.prototype._propagateFrom = function (parent, flags) {
  882. ;
  883. ;
  884. };
  885. function cancellationExecute(executor, resolve, reject) {
  886. var promise = this;
  887. try {
  888. executor(resolve, reject, function(onCancel) {
  889. if (typeof onCancel !== "function") {
  890. throw new TypeError("onCancel must be a function, got: " +
  891. util.toString(onCancel));
  892. }
  893. promise._attachCancellationCallback(onCancel);
  894. });
  895. } catch (e) {
  896. return e;
  897. }
  898. }
  899. function cancellationAttachCancellationCallback(onCancel) {
  900. if (!this._isCancellable()) return this;
  901. var previousOnCancel = this._onCancel();
  902. if (previousOnCancel !== undefined) {
  903. if (util.isArray(previousOnCancel)) {
  904. previousOnCancel.push(onCancel);
  905. } else {
  906. this._setOnCancel([previousOnCancel, onCancel]);
  907. }
  908. } else {
  909. this._setOnCancel(onCancel);
  910. }
  911. }
  912. function cancellationOnCancel() {
  913. return this._onCancelField;
  914. }
  915. function cancellationSetOnCancel(onCancel) {
  916. this._onCancelField = onCancel;
  917. }
  918. function cancellationClearCancellationData() {
  919. this._cancellationParent = undefined;
  920. this._onCancelField = undefined;
  921. }
  922. function cancellationPropagateFrom(parent, flags) {
  923. if ((flags & 1) !== 0) {
  924. this._cancellationParent = parent;
  925. var branchesRemainingToCancel = parent._branchesRemainingToCancel;
  926. if (branchesRemainingToCancel === undefined) {
  927. branchesRemainingToCancel = 0;
  928. }
  929. parent._branchesRemainingToCancel = branchesRemainingToCancel + 1;
  930. }
  931. if ((flags & 2) !== 0 && parent._isBound()) {
  932. this._setBoundTo(parent._boundTo);
  933. }
  934. }
  935. function bindingPropagateFrom(parent, flags) {
  936. if ((flags & 2) !== 0 && parent._isBound()) {
  937. this._setBoundTo(parent._boundTo);
  938. }
  939. }
  940. var propagateFromFunction = bindingPropagateFrom;
  941. function boundValueFunction() {
  942. var ret = this._boundTo;
  943. if (ret !== undefined) {
  944. if (ret instanceof Promise) {
  945. if (ret.isFulfilled()) {
  946. return ret.value();
  947. } else {
  948. return undefined;
  949. }
  950. }
  951. }
  952. return ret;
  953. }
  954. function longStackTracesCaptureStackTrace() {
  955. this._trace = new CapturedTrace(this._peekContext());
  956. }
  957. function longStackTracesAttachExtraTrace(error, ignoreSelf) {
  958. if (canAttachTrace(error)) {
  959. var trace = this._trace;
  960. if (trace !== undefined) {
  961. if (ignoreSelf) trace = trace._parent;
  962. }
  963. if (trace !== undefined) {
  964. trace.attachExtraTrace(error);
  965. } else if (!error.__stackCleaned__) {
  966. var parsed = parseStackAndMessage(error);
  967. util.notEnumerableProp(error, "stack",
  968. parsed.message + "\n" + parsed.stack.join("\n"));
  969. util.notEnumerableProp(error, "__stackCleaned__", true);
  970. }
  971. }
  972. }
  973. function checkForgottenReturns(returnValue, promiseCreated, name, promise,
  974. parent) {
  975. if (returnValue === undefined && promiseCreated !== null &&
  976. wForgottenReturn) {
  977. if (parent !== undefined && parent._returnedNonUndefined()) return;
  978. if ((promise._bitField & 65535) === 0) return;
  979. if (name) name = name + " ";
  980. var handlerLine = "";
  981. var creatorLine = "";
  982. if (promiseCreated._trace) {
  983. var traceLines = promiseCreated._trace.stack.split("\n");
  984. var stack = cleanStack(traceLines);
  985. for (var i = stack.length - 1; i >= 0; --i) {
  986. var line = stack[i];
  987. if (!nodeFramePattern.test(line)) {
  988. var lineMatches = line.match(parseLinePattern);
  989. if (lineMatches) {
  990. handlerLine = "at " + lineMatches[1] +
  991. ":" + lineMatches[2] + ":" + lineMatches[3] + " ";
  992. }
  993. break;
  994. }
  995. }
  996. if (stack.length > 0) {
  997. var firstUserLine = stack[0];
  998. for (var i = 0; i < traceLines.length; ++i) {
  999. if (traceLines[i] === firstUserLine) {
  1000. if (i > 0) {
  1001. creatorLine = "\n" + traceLines[i - 1];
  1002. }
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. }
  1008. var msg = "a promise was created in a " + name +
  1009. "handler " + handlerLine + "but was not returned from it, " +
  1010. "see http://goo.gl/rRqMUw" +
  1011. creatorLine;
  1012. promise._warn(msg, true, promiseCreated);
  1013. }
  1014. }
  1015. function deprecated(name, replacement) {
  1016. var message = name +
  1017. " is deprecated and will be removed in a future version.";
  1018. if (replacement) message += " Use " + replacement + " instead.";
  1019. return warn(message);
  1020. }
  1021. function warn(message, shouldUseOwnTrace, promise) {
  1022. if (!config.warnings) return;
  1023. var warning = new Warning(message);
  1024. var ctx;
  1025. if (shouldUseOwnTrace) {
  1026. promise._attachExtraTrace(warning);
  1027. } else if (config.longStackTraces && (ctx = Promise._peekContext())) {
  1028. ctx.attachExtraTrace(warning);
  1029. } else {
  1030. var parsed = parseStackAndMessage(warning);
  1031. warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
  1032. }
  1033. if (!activeFireEvent("warning", warning)) {
  1034. formatAndLogError(warning, "", true);
  1035. }
  1036. }
  1037. function reconstructStack(message, stacks) {
  1038. for (var i = 0; i < stacks.length - 1; ++i) {
  1039. stacks[i].push("From previous event:");
  1040. stacks[i] = stacks[i].join("\n");
  1041. }
  1042. if (i < stacks.length) {
  1043. stacks[i] = stacks[i].join("\n");
  1044. }
  1045. return message + "\n" + stacks.join("\n");
  1046. }
  1047. function removeDuplicateOrEmptyJumps(stacks) {
  1048. for (var i = 0; i < stacks.length; ++i) {
  1049. if (stacks[i].length === 0 ||
  1050. ((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) {
  1051. stacks.splice(i, 1);
  1052. i--;
  1053. }
  1054. }
  1055. }
  1056. function removeCommonRoots(stacks) {
  1057. var current = stacks[0];
  1058. for (var i = 1; i < stacks.length; ++i) {
  1059. var prev = stacks[i];
  1060. var currentLastIndex = current.length - 1;
  1061. var currentLastLine = current[currentLastIndex];
  1062. var commonRootMeetPoint = -1;
  1063. for (var j = prev.length - 1; j >= 0; --j) {
  1064. if (prev[j] === currentLastLine) {
  1065. commonRootMeetPoint = j;
  1066. break;
  1067. }
  1068. }
  1069. for (var j = commonRootMeetPoint; j >= 0; --j) {
  1070. var line = prev[j];
  1071. if (current[currentLastIndex] === line) {
  1072. current.pop();
  1073. currentLastIndex--;
  1074. } else {
  1075. break;
  1076. }
  1077. }
  1078. current = prev;
  1079. }
  1080. }
  1081. function cleanStack(stack) {
  1082. var ret = [];
  1083. for (var i = 0; i < stack.length; ++i) {
  1084. var line = stack[i];
  1085. var isTraceLine = " (No stack trace)" === line ||
  1086. stackFramePattern.test(line);
  1087. var isInternalFrame = isTraceLine && shouldIgnore(line);
  1088. if (isTraceLine && !isInternalFrame) {
  1089. if (indentStackFrames && line.charAt(0) !== " ") {
  1090. line = " " + line;
  1091. }
  1092. ret.push(line);
  1093. }
  1094. }
  1095. return ret;
  1096. }
  1097. function stackFramesAsArray(error) {
  1098. var stack = error.stack.replace(/\s+$/g, "").split("\n");
  1099. for (var i = 0; i < stack.length; ++i) {
  1100. var line = stack[i];
  1101. if (" (No stack trace)" === line || stackFramePattern.test(line)) {
  1102. break;
  1103. }
  1104. }
  1105. if (i > 0 && error.name != "SyntaxError") {
  1106. stack = stack.slice(i);
  1107. }
  1108. return stack;
  1109. }
  1110. function parseStackAndMessage(error) {
  1111. var stack = error.stack;
  1112. var message = error.toString();
  1113. stack = typeof stack === "string" && stack.length > 0
  1114. ? stackFramesAsArray(error) : [" (No stack trace)"];
  1115. return {
  1116. message: message,
  1117. stack: error.name == "SyntaxError" ? stack : cleanStack(stack)
  1118. };
  1119. }
  1120. function formatAndLogError(error, title, isSoft) {
  1121. if (typeof console !== "undefined") {
  1122. var message;
  1123. if (util.isObject(error)) {
  1124. var stack = error.stack;
  1125. message = title + formatStack(stack, error);
  1126. } else {
  1127. message = title + String(error);
  1128. }
  1129. if (typeof printWarning === "function") {
  1130. printWarning(message, isSoft);
  1131. } else if (typeof console.log === "function" ||
  1132. typeof console.log === "object") {
  1133. console.log(message);
  1134. }
  1135. }
  1136. }
  1137. function fireRejectionEvent(name, localHandler, reason, promise) {
  1138. var localEventFired = false;
  1139. try {
  1140. if (typeof localHandler === "function") {
  1141. localEventFired = true;
  1142. if (name === "rejectionHandled") {
  1143. localHandler(promise);
  1144. } else {
  1145. localHandler(reason, promise);
  1146. }
  1147. }
  1148. } catch (e) {
  1149. async.throwLater(e);
  1150. }
  1151. if (name === "unhandledRejection") {
  1152. if (!activeFireEvent(name, reason, promise) && !localEventFired) {
  1153. formatAndLogError(reason, "Unhandled rejection ");
  1154. }
  1155. } else {
  1156. activeFireEvent(name, promise);
  1157. }
  1158. }
  1159. function formatNonError(obj) {
  1160. var str;
  1161. if (typeof obj === "function") {
  1162. str = "[function " +
  1163. (obj.name || "anonymous") +
  1164. "]";
  1165. } else {
  1166. str = obj && typeof obj.toString === "function"
  1167. ? obj.toString() : util.toString(obj);
  1168. var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
  1169. if (ruselessToString.test(str)) {
  1170. try {
  1171. var newStr = JSON.stringify(obj);
  1172. str = newStr;
  1173. }
  1174. catch(e) {
  1175. }
  1176. }
  1177. if (str.length === 0) {
  1178. str = "(empty array)";
  1179. }
  1180. }
  1181. return ("(<" + snip(str) + ">, no stack trace)");
  1182. }
  1183. function snip(str) {
  1184. var maxChars = 41;
  1185. if (str.length < maxChars) {
  1186. return str;
  1187. }
  1188. return str.substr(0, maxChars - 3) + "...";
  1189. }
  1190. function longStackTracesIsSupported() {
  1191. return typeof captureStackTrace === "function";
  1192. }
  1193. var shouldIgnore = function() { return false; };
  1194. var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/;
  1195. function parseLineInfo(line) {
  1196. var matches = line.match(parseLineInfoRegex);
  1197. if (matches) {
  1198. return {
  1199. fileName: matches[1],
  1200. line: parseInt(matches[2], 10)
  1201. };
  1202. }
  1203. }
  1204. function setBounds(firstLineError, lastLineError) {
  1205. if (!longStackTracesIsSupported()) return;
  1206. var firstStackLines = firstLineError.stack.split("\n");
  1207. var lastStackLines = lastLineError.stack.split("\n");
  1208. var firstIndex = -1;
  1209. var lastIndex = -1;
  1210. var firstFileName;
  1211. var lastFileName;
  1212. for (var i = 0; i < firstStackLines.length; ++i) {
  1213. var result = parseLineInfo(firstStackLines[i]);
  1214. if (result) {
  1215. firstFileName = result.fileName;
  1216. firstIndex = result.line;
  1217. break;
  1218. }
  1219. }
  1220. for (var i = 0; i < lastStackLines.length; ++i) {
  1221. var result = parseLineInfo(lastStackLines[i]);
  1222. if (result) {
  1223. lastFileName = result.fileName;
  1224. lastIndex = result.line;
  1225. break;
  1226. }
  1227. }
  1228. if (firstIndex < 0 || lastIndex < 0 || !firstFileName || !lastFileName ||
  1229. firstFileName !== lastFileName || firstIndex >= lastIndex) {
  1230. return;
  1231. }
  1232. shouldIgnore = function(line) {
  1233. if (bluebirdFramePattern.test(line)) return true;
  1234. var info = parseLineInfo(line);
  1235. if (info) {
  1236. if (info.fileName === firstFileName &&
  1237. (firstIndex <= info.line && info.line <= lastIndex)) {
  1238. return true;
  1239. }
  1240. }
  1241. return false;
  1242. };
  1243. }
  1244. function CapturedTrace(parent) {
  1245. this._parent = parent;
  1246. this._promisesCreated = 0;
  1247. var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
  1248. captureStackTrace(this, CapturedTrace);
  1249. if (length > 32) this.uncycle();
  1250. }
  1251. util.inherits(CapturedTrace, Error);
  1252. Context.CapturedTrace = CapturedTrace;
  1253. CapturedTrace.prototype.uncycle = function() {
  1254. var length = this._length;
  1255. if (length < 2) return;
  1256. var nodes = [];
  1257. var stackToIndex = {};
  1258. for (var i = 0, node = this; node !== undefined; ++i) {
  1259. nodes.push(node);
  1260. node = node._parent;
  1261. }
  1262. length = this._length = i;
  1263. for (var i = length - 1; i >= 0; --i) {
  1264. var stack = nodes[i].stack;
  1265. if (stackToIndex[stack] === undefined) {
  1266. stackToIndex[stack] = i;
  1267. }
  1268. }
  1269. for (var i = 0; i < length; ++i) {
  1270. var currentStack = nodes[i].stack;
  1271. var index = stackToIndex[currentStack];
  1272. if (index !== undefined && index !== i) {
  1273. if (index > 0) {
  1274. nodes[index - 1]._parent = undefined;
  1275. nodes[index - 1]._length = 1;
  1276. }
  1277. nodes[i]._parent = undefined;
  1278. nodes[i]._length = 1;
  1279. var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
  1280. if (index < length - 1) {
  1281. cycleEdgeNode._parent = nodes[index + 1];
  1282. cycleEdgeNode._parent.uncycle();
  1283. cycleEdgeNode._length =
  1284. cycleEdgeNode._parent._length + 1;
  1285. } else {
  1286. cycleEdgeNode._parent = undefined;
  1287. cycleEdgeNode._length = 1;
  1288. }
  1289. var currentChildLength = cycleEdgeNode._length + 1;
  1290. for (var j = i - 2; j >= 0; --j) {
  1291. nodes[j]._length = currentChildLength;
  1292. currentChildLength++;
  1293. }
  1294. return;
  1295. }
  1296. }
  1297. };
  1298. CapturedTrace.prototype.attachExtraTrace = function(error) {
  1299. if (error.__stackCleaned__) return;
  1300. this.uncycle();
  1301. var parsed = parseStackAndMessage(error);
  1302. var message = parsed.message;
  1303. var stacks = [parsed.stack];
  1304. var trace = this;
  1305. while (trace !== undefined) {
  1306. stacks.push(cleanStack(trace.stack.split("\n")));
  1307. trace = trace._parent;
  1308. }
  1309. removeCommonRoots(stacks);
  1310. removeDuplicateOrEmptyJumps(stacks);
  1311. util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
  1312. util.notEnumerableProp(error, "__stackCleaned__", true);
  1313. };
  1314. var captureStackTrace = (function stackDetection() {
  1315. var v8stackFramePattern = /^\s*at\s*/;
  1316. var v8stackFormatter = function(stack, error) {
  1317. if (typeof stack === "string") return stack;
  1318. if (error.name !== undefined &&
  1319. error.message !== undefined) {
  1320. return error.toString();
  1321. }
  1322. return formatNonError(error);
  1323. };
  1324. if (typeof Error.stackTraceLimit === "number" &&
  1325. typeof Error.captureStackTrace === "function") {
  1326. Error.stackTraceLimit += 6;
  1327. stackFramePattern = v8stackFramePattern;
  1328. formatStack = v8stackFormatter;
  1329. var captureStackTrace = Error.captureStackTrace;
  1330. shouldIgnore = function(line) {
  1331. return bluebirdFramePattern.test(line);
  1332. };
  1333. return function(receiver, ignoreUntil) {
  1334. Error.stackTraceLimit += 6;
  1335. captureStackTrace(receiver, ignoreUntil);
  1336. Error.stackTraceLimit -= 6;
  1337. };
  1338. }
  1339. var err = new Error();
  1340. if (typeof err.stack === "string" &&
  1341. err.stack.split("\n")[0].indexOf("stackDetection@") >= 0) {
  1342. stackFramePattern = /@/;
  1343. formatStack = v8stackFormatter;
  1344. indentStackFrames = true;
  1345. return function captureStackTrace(o) {
  1346. o.stack = new Error().stack;
  1347. };
  1348. }
  1349. var hasStackAfterThrow;
  1350. try { throw new Error(); }
  1351. catch(e) {
  1352. hasStackAfterThrow = ("stack" in e);
  1353. }
  1354. if (!("stack" in err) && hasStackAfterThrow &&
  1355. typeof Error.stackTraceLimit === "number") {
  1356. stackFramePattern = v8stackFramePattern;
  1357. formatStack = v8stackFormatter;
  1358. return function captureStackTrace(o) {
  1359. Error.stackTraceLimit += 6;
  1360. try { throw new Error(); }
  1361. catch(e) { o.stack = e.stack; }
  1362. Error.stackTraceLimit -= 6;
  1363. };
  1364. }
  1365. formatStack = function(stack, error) {
  1366. if (typeof stack === "string") return stack;
  1367. if ((typeof error === "object" ||
  1368. typeof error === "function") &&
  1369. error.name !== undefined &&
  1370. error.message !== undefined) {
  1371. return error.toString();
  1372. }
  1373. return formatNonError(error);
  1374. };
  1375. return null;
  1376. })([]);
  1377. if (typeof console !== "undefined" && typeof console.warn !== "undefined") {
  1378. printWarning = function (message) {
  1379. console.warn(message);
  1380. };
  1381. if (util.isNode && process.stderr.isTTY) {
  1382. printWarning = function(message, isSoft) {
  1383. var color = isSoft ? "\u001b[33m" : "\u001b[31m";
  1384. console.warn(color + message + "\u001b[0m\n");
  1385. };
  1386. } else if (!util.isNode && typeof (new Error().stack) === "string") {
  1387. printWarning = function(message, isSoft) {
  1388. console.warn("%c" + message,
  1389. isSoft ? "color: darkorange" : "color: red");
  1390. };
  1391. }
  1392. }
  1393. var config = {
  1394. warnings: warnings,
  1395. longStackTraces: false,
  1396. cancellation: false,
  1397. monitoring: false
  1398. };
  1399. if (longStackTraces) Promise.longStackTraces();
  1400. return {
  1401. longStackTraces: function() {
  1402. return config.longStackTraces;
  1403. },
  1404. warnings: function() {
  1405. return config.warnings;
  1406. },
  1407. cancellation: function() {
  1408. return config.cancellation;
  1409. },
  1410. monitoring: function() {
  1411. return config.monitoring;
  1412. },
  1413. propagateFromFunction: function() {
  1414. return propagateFromFunction;
  1415. },
  1416. boundValueFunction: function() {
  1417. return boundValueFunction;
  1418. },
  1419. checkForgottenReturns: checkForgottenReturns,
  1420. setBounds: setBounds,
  1421. warn: warn,
  1422. deprecated: deprecated,
  1423. CapturedTrace: CapturedTrace,
  1424. fireDomEvent: fireDomEvent,
  1425. fireGlobalEvent: fireGlobalEvent
  1426. };
  1427. };
  1428. },{"./errors":12,"./util":36}],10:[function(_dereq_,module,exports){
  1429. "use strict";
  1430. module.exports = function(Promise) {
  1431. function returner() {
  1432. return this.value;
  1433. }
  1434. function thrower() {
  1435. throw this.reason;
  1436. }
  1437. Promise.prototype["return"] =
  1438. Promise.prototype.thenReturn = function (value) {
  1439. if (value instanceof Promise) value.suppressUnhandledRejections();
  1440. return this._then(
  1441. returner, undefined, undefined, {value: value}, undefined);
  1442. };
  1443. Promise.prototype["throw"] =
  1444. Promise.prototype.thenThrow = function (reason) {
  1445. return this._then(
  1446. thrower, undefined, undefined, {reason: reason}, undefined);
  1447. };
  1448. Promise.prototype.catchThrow = function (reason) {
  1449. if (arguments.length <= 1) {
  1450. return this._then(
  1451. undefined, thrower, undefined, {reason: reason}, undefined);
  1452. } else {
  1453. var _reason = arguments[1];
  1454. var handler = function() {throw _reason;};
  1455. return this.caught(reason, handler);
  1456. }
  1457. };
  1458. Promise.prototype.catchReturn = function (value) {
  1459. if (arguments.length <= 1) {
  1460. if (value instanceof Promise) value.suppressUnhandledRejections();
  1461. return this._then(
  1462. undefined, returner, undefined, {value: value}, undefined);
  1463. } else {
  1464. var _value = arguments[1];
  1465. if (_value instanceof Promise) _value.suppressUnhandledRejections();
  1466. var handler = function() {return _value;};
  1467. return this.caught(value, handler);
  1468. }
  1469. };
  1470. };
  1471. },{}],11:[function(_dereq_,module,exports){
  1472. "use strict";
  1473. module.exports = function(Promise, INTERNAL) {
  1474. var PromiseReduce = Promise.reduce;
  1475. var PromiseAll = Promise.all;
  1476. function promiseAllThis() {
  1477. return PromiseAll(this);
  1478. }
  1479. function PromiseMapSeries(promises, fn) {
  1480. return PromiseReduce(promises, fn, INTERNAL, INTERNAL);
  1481. }
  1482. Promise.prototype.each = function (fn) {
  1483. return PromiseReduce(this, fn, INTERNAL, 0)
  1484. ._then(promiseAllThis, undefined, undefined, this, undefined);
  1485. };
  1486. Promise.prototype.mapSeries = function (fn) {
  1487. return PromiseReduce(this, fn, INTERNAL, INTERNAL);
  1488. };
  1489. Promise.each = function (promises, fn) {
  1490. return PromiseReduce(promises, fn, INTERNAL, 0)
  1491. ._then(promiseAllThis, undefined, undefined, promises, undefined);
  1492. };
  1493. Promise.mapSeries = PromiseMapSeries;
  1494. };
  1495. },{}],12:[function(_dereq_,module,exports){
  1496. "use strict";
  1497. var es5 = _dereq_("./es5");
  1498. var Objectfreeze = es5.freeze;
  1499. var util = _dereq_("./util");
  1500. var inherits = util.inherits;
  1501. var notEnumerableProp = util.notEnumerableProp;
  1502. function subError(nameProperty, defaultMessage) {
  1503. function SubError(message) {
  1504. if (!(this instanceof SubError)) return new SubError(message);
  1505. notEnumerableProp(this, "message",
  1506. typeof message === "string" ? message : defaultMessage);
  1507. notEnumerableProp(this, "name", nameProperty);
  1508. if (Error.captureStackTrace) {
  1509. Error.captureStackTrace(this, this.constructor);
  1510. } else {
  1511. Error.call(this);
  1512. }
  1513. }
  1514. inherits(SubError, Error);
  1515. return SubError;
  1516. }
  1517. var _TypeError, _RangeError;
  1518. var Warning = subError("Warning", "warning");
  1519. var CancellationError = subError("CancellationError", "cancellation error");
  1520. var TimeoutError = subError("TimeoutError", "timeout error");
  1521. var AggregateError = subError("AggregateError", "aggregate error");
  1522. try {
  1523. _TypeError = TypeError;
  1524. _RangeError = RangeError;
  1525. } catch(e) {
  1526. _TypeError = subError("TypeError", "type error");
  1527. _RangeError = subError("RangeError", "range error");
  1528. }
  1529. var methods = ("join pop push shift unshift slice filter forEach some " +
  1530. "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" ");
  1531. for (var i = 0; i < methods.length; ++i) {
  1532. if (typeof Array.prototype[methods[i]] === "function") {
  1533. AggregateError.prototype[methods[i]] = Array.prototype[methods[i]];
  1534. }
  1535. }
  1536. es5.defineProperty(AggregateError.prototype, "length", {
  1537. value: 0,
  1538. configurable: false,
  1539. writable: true,
  1540. enumerable: true
  1541. });
  1542. AggregateError.prototype["isOperational"] = true;
  1543. var level = 0;
  1544. AggregateError.prototype.toString = function() {
  1545. var indent = Array(level * 4 + 1).join(" ");
  1546. var ret = "\n" + indent + "AggregateError of:" + "\n";
  1547. level++;
  1548. indent = Array(level * 4 + 1).join(" ");
  1549. for (var i = 0; i < this.length; ++i) {
  1550. var str = this[i] === this ? "[Circular AggregateError]" : this[i] + "";
  1551. var lines = str.split("\n");
  1552. for (var j = 0; j < lines.length; ++j) {
  1553. lines[j] = indent + lines[j];
  1554. }
  1555. str = lines.join("\n");
  1556. ret += str + "\n";
  1557. }
  1558. level--;
  1559. return ret;
  1560. };
  1561. function OperationalError(message) {
  1562. if (!(this instanceof OperationalError))
  1563. return new OperationalError(message);
  1564. notEnumerableProp(this, "name", "OperationalError");
  1565. notEnumerableProp(this, "message", message);
  1566. this.cause = message;
  1567. this["isOperational"] = true;
  1568. if (message instanceof Error) {
  1569. notEnumerableProp(this, "message", message.message);
  1570. notEnumerableProp(this, "stack", message.stack);
  1571. } else if (Error.captureStackTrace) {
  1572. Error.captureStackTrace(this, this.constructor);
  1573. }
  1574. }
  1575. inherits(OperationalError, Error);
  1576. var errorTypes = Error["__BluebirdErrorTypes__"];
  1577. if (!errorTypes) {
  1578. errorTypes = Objectfreeze({
  1579. CancellationError: CancellationError,
  1580. TimeoutError: TimeoutError,
  1581. OperationalError: OperationalError,
  1582. RejectionError: OperationalError,
  1583. AggregateError: AggregateError
  1584. });
  1585. es5.defineProperty(Error, "__BluebirdErrorTypes__", {
  1586. value: errorTypes,
  1587. writable: false,
  1588. enumerable: false,
  1589. configurable: false
  1590. });
  1591. }
  1592. module.exports = {
  1593. Error: Error,
  1594. TypeError: _TypeError,
  1595. RangeError: _RangeError,
  1596. CancellationError: errorTypes.CancellationError,
  1597. OperationalError: errorTypes.OperationalError,
  1598. TimeoutError: errorTypes.TimeoutError,
  1599. AggregateError: errorTypes.AggregateError,
  1600. Warning: Warning
  1601. };
  1602. },{"./es5":13,"./util":36}],13:[function(_dereq_,module,exports){
  1603. var isES5 = (function(){
  1604. "use strict";
  1605. return this === undefined;
  1606. })();
  1607. if (isES5) {
  1608. module.exports = {
  1609. freeze: Object.freeze,
  1610. defineProperty: Object.defineProperty,
  1611. getDescriptor: Object.getOwnPropertyDescriptor,
  1612. keys: Object.keys,
  1613. names: Object.getOwnPropertyNames,
  1614. getPrototypeOf: Object.getPrototypeOf,
  1615. isArray: Array.isArray,
  1616. isES5: isES5,
  1617. propertyIsWritable: function(obj, prop) {
  1618. var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
  1619. return !!(!descriptor || descriptor.writable || descriptor.set);
  1620. }
  1621. };
  1622. } else {
  1623. var has = {}.hasOwnProperty;
  1624. var str = {}.toString;
  1625. var proto = {}.constructor.prototype;
  1626. var ObjectKeys = function (o) {
  1627. var ret = [];
  1628. for (var key in o) {
  1629. if (has.call(o, key)) {
  1630. ret.push(key);
  1631. }
  1632. }
  1633. return ret;
  1634. };
  1635. var ObjectGetDescriptor = function(o, key) {
  1636. return {value: o[key]};
  1637. };
  1638. var ObjectDefineProperty = function (o, key, desc) {
  1639. o[key] = desc.value;
  1640. return o;
  1641. };
  1642. var ObjectFreeze = function (obj) {
  1643. return obj;
  1644. };
  1645. var ObjectGetPrototypeOf = function (obj) {
  1646. try {
  1647. return Object(obj).constructor.prototype;
  1648. }
  1649. catch (e) {
  1650. return proto;
  1651. }
  1652. };
  1653. var ArrayIsArray = function (obj) {
  1654. try {
  1655. return str.call(obj) === "[object Array]";
  1656. }
  1657. catch(e) {
  1658. return false;
  1659. }
  1660. };
  1661. module.exports = {
  1662. isArray: ArrayIsArray,
  1663. keys: ObjectKeys,
  1664. names: ObjectKeys,
  1665. defineProperty: ObjectDefineProperty,
  1666. getDescriptor: ObjectGetDescriptor,
  1667. freeze: ObjectFreeze,
  1668. getPrototypeOf: ObjectGetPrototypeOf,
  1669. isES5: isES5,
  1670. propertyIsWritable: function() {
  1671. return true;
  1672. }
  1673. };
  1674. }
  1675. },{}],14:[function(_dereq_,module,exports){
  1676. "use strict";
  1677. module.exports = function(Promise, INTERNAL) {
  1678. var PromiseMap = Promise.map;
  1679. Promise.prototype.filter = function (fn, options) {
  1680. return PromiseMap(this, fn, options, INTERNAL);
  1681. };
  1682. Promise.filter = function (promises, fn, options) {
  1683. return PromiseMap(promises, fn, options, INTERNAL);
  1684. };
  1685. };
  1686. },{}],15:[function(_dereq_,module,exports){
  1687. "use strict";
  1688. module.exports = function(Promise, tryConvertToPromise, NEXT_FILTER) {
  1689. var util = _dereq_("./util");
  1690. var CancellationError = Promise.CancellationError;
  1691. var errorObj = util.errorObj;
  1692. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  1693. function PassThroughHandlerContext(promise, type, handler) {
  1694. this.promise = promise;
  1695. this.type = type;
  1696. this.handler = handler;
  1697. this.called = false;
  1698. this.cancelPromise = null;
  1699. }
  1700. PassThroughHandlerContext.prototype.isFinallyHandler = function() {
  1701. return this.type === 0;
  1702. };
  1703. function FinallyHandlerCancelReaction(finallyHandler) {
  1704. this.finallyHandler = finallyHandler;
  1705. }
  1706. FinallyHandlerCancelReaction.prototype._resultCancelled = function() {
  1707. checkCancel(this.finallyHandler);
  1708. };
  1709. function checkCancel(ctx, reason) {
  1710. if (ctx.cancelPromise != null) {
  1711. if (arguments.length > 1) {
  1712. ctx.cancelPromise._reject(reason);
  1713. } else {
  1714. ctx.cancelPromise._cancel();
  1715. }
  1716. ctx.cancelPromise = null;
  1717. return true;
  1718. }
  1719. return false;
  1720. }
  1721. function succeed() {
  1722. return finallyHandler.call(this, this.promise._target()._settledValue());
  1723. }
  1724. function fail(reason) {
  1725. if (checkCancel(this, reason)) return;
  1726. errorObj.e = reason;
  1727. return errorObj;
  1728. }
  1729. function finallyHandler(reasonOrValue) {
  1730. var promise = this.promise;
  1731. var handler = this.handler;
  1732. if (!this.called) {
  1733. this.called = true;
  1734. var ret = this.isFinallyHandler()
  1735. ? handler.call(promise._boundValue())
  1736. : handler.call(promise._boundValue(), reasonOrValue);
  1737. if (ret === NEXT_FILTER) {
  1738. return ret;
  1739. } else if (ret !== undefined) {
  1740. promise._setReturnedNonUndefined();
  1741. var maybePromise = tryConvertToPromise(ret, promise);
  1742. if (maybePromise instanceof Promise) {
  1743. if (this.cancelPromise != null) {
  1744. if (maybePromise._isCancelled()) {
  1745. var reason =
  1746. new CancellationError("late cancellation observer");
  1747. promise._attachExtraTrace(reason);
  1748. errorObj.e = reason;
  1749. return errorObj;
  1750. } else if (maybePromise.isPending()) {
  1751. maybePromise._attachCancellationCallback(
  1752. new FinallyHandlerCancelReaction(this));
  1753. }
  1754. }
  1755. return maybePromise._then(
  1756. succeed, fail, undefined, this, undefined);
  1757. }
  1758. }
  1759. }
  1760. if (promise.isRejected()) {
  1761. checkCancel(this);
  1762. errorObj.e = reasonOrValue;
  1763. return errorObj;
  1764. } else {
  1765. checkCancel(this);
  1766. return reasonOrValue;
  1767. }
  1768. }
  1769. Promise.prototype._passThrough = function(handler, type, success, fail) {
  1770. if (typeof handler !== "function") return this.then();
  1771. return this._then(success,
  1772. fail,
  1773. undefined,
  1774. new PassThroughHandlerContext(this, type, handler),
  1775. undefined);
  1776. };
  1777. Promise.prototype.lastly =
  1778. Promise.prototype["finally"] = function (handler) {
  1779. return this._passThrough(handler,
  1780. 0,
  1781. finallyHandler,
  1782. finallyHandler);
  1783. };
  1784. Promise.prototype.tap = function (handler) {
  1785. return this._passThrough(handler, 1, finallyHandler);
  1786. };
  1787. Promise.prototype.tapCatch = function (handlerOrPredicate) {
  1788. var len = arguments.length;
  1789. if(len === 1) {
  1790. return this._passThrough(handlerOrPredicate,
  1791. 1,
  1792. undefined,
  1793. finallyHandler);
  1794. } else {
  1795. var catchInstances = new Array(len - 1),
  1796. j = 0, i;
  1797. for (i = 0; i < len - 1; ++i) {
  1798. var item = arguments[i];
  1799. if (util.isObject(item)) {
  1800. catchInstances[j++] = item;
  1801. } else {
  1802. return Promise.reject(new TypeError(
  1803. "tapCatch statement predicate: "
  1804. + "expecting an object but got " + util.classString(item)
  1805. ));
  1806. }
  1807. }
  1808. catchInstances.length = j;
  1809. var handler = arguments[i];
  1810. return this._passThrough(catchFilter(catchInstances, handler, this),
  1811. 1,
  1812. undefined,
  1813. finallyHandler);
  1814. }
  1815. };
  1816. return PassThroughHandlerContext;
  1817. };
  1818. },{"./catch_filter":7,"./util":36}],16:[function(_dereq_,module,exports){
  1819. "use strict";
  1820. module.exports = function(Promise,
  1821. apiRejection,
  1822. INTERNAL,
  1823. tryConvertToPromise,
  1824. Proxyable,
  1825. debug) {
  1826. var errors = _dereq_("./errors");
  1827. var TypeError = errors.TypeError;
  1828. var util = _dereq_("./util");
  1829. var errorObj = util.errorObj;
  1830. var tryCatch = util.tryCatch;
  1831. var yieldHandlers = [];
  1832. function promiseFromYieldHandler(value, yieldHandlers, traceParent) {
  1833. for (var i = 0; i < yieldHandlers.length; ++i) {
  1834. traceParent._pushContext();
  1835. var result = tryCatch(yieldHandlers[i])(value);
  1836. traceParent._popContext();
  1837. if (result === errorObj) {
  1838. traceParent._pushContext();
  1839. var ret = Promise.reject(errorObj.e);
  1840. traceParent._popContext();
  1841. return ret;
  1842. }
  1843. var maybePromise = tryConvertToPromise(result, traceParent);
  1844. if (maybePromise instanceof Promise) return maybePromise;
  1845. }
  1846. return null;
  1847. }
  1848. function PromiseSpawn(generatorFunction, receiver, yieldHandler, stack) {
  1849. if (debug.cancellation()) {
  1850. var internal = new Promise(INTERNAL);
  1851. var _finallyPromise = this._finallyPromise = new Promise(INTERNAL);
  1852. this._promise = internal.lastly(function() {
  1853. return _finallyPromise;
  1854. });
  1855. internal._captureStackTrace();
  1856. internal._setOnCancel(this);
  1857. } else {
  1858. var promise = this._promise = new Promise(INTERNAL);
  1859. promise._captureStackTrace();
  1860. }
  1861. this._stack = stack;
  1862. this._generatorFunction = generatorFunction;
  1863. this._receiver = receiver;
  1864. this._generator = undefined;
  1865. this._yieldHandlers = typeof yieldHandler === "function"
  1866. ? [yieldHandler].concat(yieldHandlers)
  1867. : yieldHandlers;
  1868. this._yieldedPromise = null;
  1869. this._cancellationPhase = false;
  1870. }
  1871. util.inherits(PromiseSpawn, Proxyable);
  1872. PromiseSpawn.prototype._isResolved = function() {
  1873. return this._promise === null;
  1874. };
  1875. PromiseSpawn.prototype._cleanup = function() {
  1876. this._promise = this._generator = null;
  1877. if (debug.cancellation() && this._finallyPromise !== null) {
  1878. this._finallyPromise._fulfill();
  1879. this._finallyPromise = null;
  1880. }
  1881. };
  1882. PromiseSpawn.prototype._promiseCancelled = function() {
  1883. if (this._isResolved()) return;
  1884. var implementsReturn = typeof this._generator["return"] !== "undefined";
  1885. var result;
  1886. if (!implementsReturn) {
  1887. var reason = new Promise.CancellationError(
  1888. "generator .return() sentinel");
  1889. Promise.coroutine.returnSentinel = reason;
  1890. this._promise._attachExtraTrace(reason);
  1891. this._promise._pushContext();
  1892. result = tryCatch(this._generator["throw"]).call(this._generator,
  1893. reason);
  1894. this._promise._popContext();
  1895. } else {
  1896. this._promise._pushContext();
  1897. result = tryCatch(this._generator["return"]).call(this._generator,
  1898. undefined);
  1899. this._promise._popContext();
  1900. }
  1901. this._cancellationPhase = true;
  1902. this._yieldedPromise = null;
  1903. this._continue(result);
  1904. };
  1905. PromiseSpawn.prototype._promiseFulfilled = function(value) {
  1906. this._yieldedPromise = null;
  1907. this._promise._pushContext();
  1908. var result = tryCatch(this._generator.next).call(this._generator, value);
  1909. this._promise._popContext();
  1910. this._continue(result);
  1911. };
  1912. PromiseSpawn.prototype._promiseRejected = function(reason) {
  1913. this._yieldedPromise = null;
  1914. this._promise._attachExtraTrace(reason);
  1915. this._promise._pushContext();
  1916. var result = tryCatch(this._generator["throw"])
  1917. .call(this._generator, reason);
  1918. this._promise._popContext();
  1919. this._continue(result);
  1920. };
  1921. PromiseSpawn.prototype._resultCancelled = function() {
  1922. if (this._yieldedPromise instanceof Promise) {
  1923. var promise = this._yieldedPromise;
  1924. this._yieldedPromise = null;
  1925. promise.cancel();
  1926. }
  1927. };
  1928. PromiseSpawn.prototype.promise = function () {
  1929. return this._promise;
  1930. };
  1931. PromiseSpawn.prototype._run = function () {
  1932. this._generator = this._generatorFunction.call(this._receiver);
  1933. this._receiver =
  1934. this._generatorFunction = undefined;
  1935. this._promiseFulfilled(undefined);
  1936. };
  1937. PromiseSpawn.prototype._continue = function (result) {
  1938. var promise = this._promise;
  1939. if (result === errorObj) {
  1940. this._cleanup();
  1941. if (this._cancellationPhase) {
  1942. return promise.cancel();
  1943. } else {
  1944. return promise._rejectCallback(result.e, false);
  1945. }
  1946. }
  1947. var value = result.value;
  1948. if (result.done === true) {
  1949. this._cleanup();
  1950. if (this._cancellationPhase) {
  1951. return promise.cancel();
  1952. } else {
  1953. return promise._resolveCallback(value);
  1954. }
  1955. } else {
  1956. var maybePromise = tryConvertToPromise(value, this._promise);
  1957. if (!(maybePromise instanceof Promise)) {
  1958. maybePromise =
  1959. promiseFromYieldHandler(maybePromise,
  1960. this._yieldHandlers,
  1961. this._promise);
  1962. if (maybePromise === null) {
  1963. this._promiseRejected(
  1964. new TypeError(
  1965. "A value %s was yielded that could not be treated as a promise\u000a\u000a See http://goo.gl/MqrFmX\u000a\u000a".replace("%s", String(value)) +
  1966. "From coroutine:\u000a" +
  1967. this._stack.split("\n").slice(1, -7).join("\n")
  1968. )
  1969. );
  1970. return;
  1971. }
  1972. }
  1973. maybePromise = maybePromise._target();
  1974. var bitField = maybePromise._bitField;
  1975. ;
  1976. if (((bitField & 50397184) === 0)) {
  1977. this._yieldedPromise = maybePromise;
  1978. maybePromise._proxy(this, null);
  1979. } else if (((bitField & 33554432) !== 0)) {
  1980. Promise._async.invoke(
  1981. this._promiseFulfilled, this, maybePromise._value()
  1982. );
  1983. } else if (((bitField & 16777216) !== 0)) {
  1984. Promise._async.invoke(
  1985. this._promiseRejected, this, maybePromise._reason()
  1986. );
  1987. } else {
  1988. this._promiseCancelled();
  1989. }
  1990. }
  1991. };
  1992. Promise.coroutine = function (generatorFunction, options) {
  1993. if (typeof generatorFunction !== "function") {
  1994. throw new TypeError("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  1995. }
  1996. var yieldHandler = Object(options).yieldHandler;
  1997. var PromiseSpawn$ = PromiseSpawn;
  1998. var stack = new Error().stack;
  1999. return function () {
  2000. var generator = generatorFunction.apply(this, arguments);
  2001. var spawn = new PromiseSpawn$(undefined, undefined, yieldHandler,
  2002. stack);
  2003. var ret = spawn.promise();
  2004. spawn._generator = generator;
  2005. spawn._promiseFulfilled(undefined);
  2006. return ret;
  2007. };
  2008. };
  2009. Promise.coroutine.addYieldHandler = function(fn) {
  2010. if (typeof fn !== "function") {
  2011. throw new TypeError("expecting a function but got " + util.classString(fn));
  2012. }
  2013. yieldHandlers.push(fn);
  2014. };
  2015. Promise.spawn = function (generatorFunction) {
  2016. debug.deprecated("Promise.spawn()", "Promise.coroutine()");
  2017. if (typeof generatorFunction !== "function") {
  2018. return apiRejection("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2019. }
  2020. var spawn = new PromiseSpawn(generatorFunction, this);
  2021. var ret = spawn.promise();
  2022. spawn._run(Promise.spawn);
  2023. return ret;
  2024. };
  2025. };
  2026. },{"./errors":12,"./util":36}],17:[function(_dereq_,module,exports){
  2027. "use strict";
  2028. module.exports =
  2029. function(Promise, PromiseArray, tryConvertToPromise, INTERNAL, async,
  2030. getDomain) {
  2031. var util = _dereq_("./util");
  2032. var canEvaluate = util.canEvaluate;
  2033. var tryCatch = util.tryCatch;
  2034. var errorObj = util.errorObj;
  2035. var reject;
  2036. if (!true) {
  2037. if (canEvaluate) {
  2038. var thenCallback = function(i) {
  2039. return new Function("value", "holder", " \n\
  2040. 'use strict'; \n\
  2041. holder.pIndex = value; \n\
  2042. holder.checkFulfillment(this); \n\
  2043. ".replace(/Index/g, i));
  2044. };
  2045. var promiseSetter = function(i) {
  2046. return new Function("promise", "holder", " \n\
  2047. 'use strict'; \n\
  2048. holder.pIndex = promise; \n\
  2049. ".replace(/Index/g, i));
  2050. };
  2051. var generateHolderClass = function(total) {
  2052. var props = new Array(total);
  2053. for (var i = 0; i < props.length; ++i) {
  2054. props[i] = "this.p" + (i+1);
  2055. }
  2056. var assignment = props.join(" = ") + " = null;";
  2057. var cancellationCode= "var promise;\n" + props.map(function(prop) {
  2058. return " \n\
  2059. promise = " + prop + "; \n\
  2060. if (promise instanceof Promise) { \n\
  2061. promise.cancel(); \n\
  2062. } \n\
  2063. ";
  2064. }).join("\n");
  2065. var passedArguments = props.join(", ");
  2066. var name = "Holder$" + total;
  2067. var code = "return function(tryCatch, errorObj, Promise, async) { \n\
  2068. 'use strict'; \n\
  2069. function [TheName](fn) { \n\
  2070. [TheProperties] \n\
  2071. this.fn = fn; \n\
  2072. this.asyncNeeded = true; \n\
  2073. this.now = 0; \n\
  2074. } \n\
  2075. \n\
  2076. [TheName].prototype._callFunction = function(promise) { \n\
  2077. promise._pushContext(); \n\
  2078. var ret = tryCatch(this.fn)([ThePassedArguments]); \n\
  2079. promise._popContext(); \n\
  2080. if (ret === errorObj) { \n\
  2081. promise._rejectCallback(ret.e, false); \n\
  2082. } else { \n\
  2083. promise._resolveCallback(ret); \n\
  2084. } \n\
  2085. }; \n\
  2086. \n\
  2087. [TheName].prototype.checkFulfillment = function(promise) { \n\
  2088. var now = ++this.now; \n\
  2089. if (now === [TheTotal]) { \n\
  2090. if (this.asyncNeeded) { \n\
  2091. async.invoke(this._callFunction, this, promise); \n\
  2092. } else { \n\
  2093. this._callFunction(promise); \n\
  2094. } \n\
  2095. \n\
  2096. } \n\
  2097. }; \n\
  2098. \n\
  2099. [TheName].prototype._resultCancelled = function() { \n\
  2100. [CancellationCode] \n\
  2101. }; \n\
  2102. \n\
  2103. return [TheName]; \n\
  2104. }(tryCatch, errorObj, Promise, async); \n\
  2105. ";
  2106. code = code.replace(/\[TheName\]/g, name)
  2107. .replace(/\[TheTotal\]/g, total)
  2108. .replace(/\[ThePassedArguments\]/g, passedArguments)
  2109. .replace(/\[TheProperties\]/g, assignment)
  2110. .replace(/\[CancellationCode\]/g, cancellationCode);
  2111. return new Function("tryCatch", "errorObj", "Promise", "async", code)
  2112. (tryCatch, errorObj, Promise, async);
  2113. };
  2114. var holderClasses = [];
  2115. var thenCallbacks = [];
  2116. var promiseSetters = [];
  2117. for (var i = 0; i < 8; ++i) {
  2118. holderClasses.push(generateHolderClass(i + 1));
  2119. thenCallbacks.push(thenCallback(i + 1));
  2120. promiseSetters.push(promiseSetter(i + 1));
  2121. }
  2122. reject = function (reason) {
  2123. this._reject(reason);
  2124. };
  2125. }}
  2126. Promise.join = function () {
  2127. var last = arguments.length - 1;
  2128. var fn;
  2129. if (last > 0 && typeof arguments[last] === "function") {
  2130. fn = arguments[last];
  2131. if (!true) {
  2132. if (last <= 8 && canEvaluate) {
  2133. var ret = new Promise(INTERNAL);
  2134. ret._captureStackTrace();
  2135. var HolderClass = holderClasses[last - 1];
  2136. var holder = new HolderClass(fn);
  2137. var callbacks = thenCallbacks;
  2138. for (var i = 0; i < last; ++i) {
  2139. var maybePromise = tryConvertToPromise(arguments[i], ret);
  2140. if (maybePromise instanceof Promise) {
  2141. maybePromise = maybePromise._target();
  2142. var bitField = maybePromise._bitField;
  2143. ;
  2144. if (((bitField & 50397184) === 0)) {
  2145. maybePromise._then(callbacks[i], reject,
  2146. undefined, ret, holder);
  2147. promiseSetters[i](maybePromise, holder);
  2148. holder.asyncNeeded = false;
  2149. } else if (((bitField & 33554432) !== 0)) {
  2150. callbacks[i].call(ret,
  2151. maybePromise._value(), holder);
  2152. } else if (((bitField & 16777216) !== 0)) {
  2153. ret._reject(maybePromise._reason());
  2154. } else {
  2155. ret._cancel();
  2156. }
  2157. } else {
  2158. callbacks[i].call(ret, maybePromise, holder);
  2159. }
  2160. }
  2161. if (!ret._isFateSealed()) {
  2162. if (holder.asyncNeeded) {
  2163. var domain = getDomain();
  2164. if (domain !== null) {
  2165. holder.fn = util.domainBind(domain, holder.fn);
  2166. }
  2167. }
  2168. ret._setAsyncGuaranteed();
  2169. ret._setOnCancel(holder);
  2170. }
  2171. return ret;
  2172. }
  2173. }
  2174. }
  2175. var args = [].slice.call(arguments);;
  2176. if (fn) args.pop();
  2177. var ret = new PromiseArray(args).promise();
  2178. return fn !== undefined ? ret.spread(fn) : ret;
  2179. };
  2180. };
  2181. },{"./util":36}],18:[function(_dereq_,module,exports){
  2182. "use strict";
  2183. module.exports = function(Promise,
  2184. PromiseArray,
  2185. apiRejection,
  2186. tryConvertToPromise,
  2187. INTERNAL,
  2188. debug) {
  2189. var getDomain = Promise._getDomain;
  2190. var util = _dereq_("./util");
  2191. var tryCatch = util.tryCatch;
  2192. var errorObj = util.errorObj;
  2193. var async = Promise._async;
  2194. function MappingPromiseArray(promises, fn, limit, _filter) {
  2195. this.constructor$(promises);
  2196. this._promise._captureStackTrace();
  2197. var domain = getDomain();
  2198. this._callback = domain === null ? fn : util.domainBind(domain, fn);
  2199. this._preservedValues = _filter === INTERNAL
  2200. ? new Array(this.length())
  2201. : null;
  2202. this._limit = limit;
  2203. this._inFlight = 0;
  2204. this._queue = [];
  2205. async.invoke(this._asyncInit, this, undefined);
  2206. }
  2207. util.inherits(MappingPromiseArray, PromiseArray);
  2208. MappingPromiseArray.prototype._asyncInit = function() {
  2209. this._init$(undefined, -2);
  2210. };
  2211. MappingPromiseArray.prototype._init = function () {};
  2212. MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
  2213. var values = this._values;
  2214. var length = this.length();
  2215. var preservedValues = this._preservedValues;
  2216. var limit = this._limit;
  2217. if (index < 0) {
  2218. index = (index * -1) - 1;
  2219. values[index] = value;
  2220. if (limit >= 1) {
  2221. this._inFlight--;
  2222. this._drainQueue();
  2223. if (this._isResolved()) return true;
  2224. }
  2225. } else {
  2226. if (limit >= 1 && this._inFlight >= limit) {
  2227. values[index] = value;
  2228. this._queue.push(index);
  2229. return false;
  2230. }
  2231. if (preservedValues !== null) preservedValues[index] = value;
  2232. var promise = this._promise;
  2233. var callback = this._callback;
  2234. var receiver = promise._boundValue();
  2235. promise._pushContext();
  2236. var ret = tryCatch(callback).call(receiver, value, index, length);
  2237. var promiseCreated = promise._popContext();
  2238. debug.checkForgottenReturns(
  2239. ret,
  2240. promiseCreated,
  2241. preservedValues !== null ? "Promise.filter" : "Promise.map",
  2242. promise
  2243. );
  2244. if (ret === errorObj) {
  2245. this._reject(ret.e);
  2246. return true;
  2247. }
  2248. var maybePromise = tryConvertToPromise(ret, this._promise);
  2249. if (maybePromise instanceof Promise) {
  2250. maybePromise = maybePromise._target();
  2251. var bitField = maybePromise._bitField;
  2252. ;
  2253. if (((bitField & 50397184) === 0)) {
  2254. if (limit >= 1) this._inFlight++;
  2255. values[index] = maybePromise;
  2256. maybePromise._proxy(this, (index + 1) * -1);
  2257. return false;
  2258. } else if (((bitField & 33554432) !== 0)) {
  2259. ret = maybePromise._value();
  2260. } else if (((bitField & 16777216) !== 0)) {
  2261. this._reject(maybePromise._reason());
  2262. return true;
  2263. } else {
  2264. this._cancel();
  2265. return true;
  2266. }
  2267. }
  2268. values[index] = ret;
  2269. }
  2270. var totalResolved = ++this._totalResolved;
  2271. if (totalResolved >= length) {
  2272. if (preservedValues !== null) {
  2273. this._filter(values, preservedValues);
  2274. } else {
  2275. this._resolve(values);
  2276. }
  2277. return true;
  2278. }
  2279. return false;
  2280. };
  2281. MappingPromiseArray.prototype._drainQueue = function () {
  2282. var queue = this._queue;
  2283. var limit = this._limit;
  2284. var values = this._values;
  2285. while (queue.length > 0 && this._inFlight < limit) {
  2286. if (this._isResolved()) return;
  2287. var index = queue.pop();
  2288. this._promiseFulfilled(values[index], index);
  2289. }
  2290. };
  2291. MappingPromiseArray.prototype._filter = function (booleans, values) {
  2292. var len = values.length;
  2293. var ret = new Array(len);
  2294. var j = 0;
  2295. for (var i = 0; i < len; ++i) {
  2296. if (booleans[i]) ret[j++] = values[i];
  2297. }
  2298. ret.length = j;
  2299. this._resolve(ret);
  2300. };
  2301. MappingPromiseArray.prototype.preservedValues = function () {
  2302. return this._preservedValues;
  2303. };
  2304. function map(promises, fn, options, _filter) {
  2305. if (typeof fn !== "function") {
  2306. return apiRejection("expecting a function but got " + util.classString(fn));
  2307. }
  2308. var limit = 0;
  2309. if (options !== undefined) {
  2310. if (typeof options === "object" && options !== null) {
  2311. if (typeof options.concurrency !== "number") {
  2312. return Promise.reject(
  2313. new TypeError("'concurrency' must be a number but it is " +
  2314. util.classString(options.concurrency)));
  2315. }
  2316. limit = options.concurrency;
  2317. } else {
  2318. return Promise.reject(new TypeError(
  2319. "options argument must be an object but it is " +
  2320. util.classString(options)));
  2321. }
  2322. }
  2323. limit = typeof limit === "number" &&
  2324. isFinite(limit) && limit >= 1 ? limit : 0;
  2325. return new MappingPromiseArray(promises, fn, limit, _filter).promise();
  2326. }
  2327. Promise.prototype.map = function (fn, options) {
  2328. return map(this, fn, options, null);
  2329. };
  2330. Promise.map = function (promises, fn, options, _filter) {
  2331. return map(promises, fn, options, _filter);
  2332. };
  2333. };
  2334. },{"./util":36}],19:[function(_dereq_,module,exports){
  2335. "use strict";
  2336. module.exports =
  2337. function(Promise, INTERNAL, tryConvertToPromise, apiRejection, debug) {
  2338. var util = _dereq_("./util");
  2339. var tryCatch = util.tryCatch;
  2340. Promise.method = function (fn) {
  2341. if (typeof fn !== "function") {
  2342. throw new Promise.TypeError("expecting a function but got " + util.classString(fn));
  2343. }
  2344. return function () {
  2345. var ret = new Promise(INTERNAL);
  2346. ret._captureStackTrace();
  2347. ret._pushContext();
  2348. var value = tryCatch(fn).apply(this, arguments);
  2349. var promiseCreated = ret._popContext();
  2350. debug.checkForgottenReturns(
  2351. value, promiseCreated, "Promise.method", ret);
  2352. ret._resolveFromSyncValue(value);
  2353. return ret;
  2354. };
  2355. };
  2356. Promise.attempt = Promise["try"] = function (fn) {
  2357. if (typeof fn !== "function") {
  2358. return apiRejection("expecting a function but got " + util.classString(fn));
  2359. }
  2360. var ret = new Promise(INTERNAL);
  2361. ret._captureStackTrace();
  2362. ret._pushContext();
  2363. var value;
  2364. if (arguments.length > 1) {
  2365. debug.deprecated("calling Promise.try with more than 1 argument");
  2366. var arg = arguments[1];
  2367. var ctx = arguments[2];
  2368. value = util.isArray(arg) ? tryCatch(fn).apply(ctx, arg)
  2369. : tryCatch(fn).call(ctx, arg);
  2370. } else {
  2371. value = tryCatch(fn)();
  2372. }
  2373. var promiseCreated = ret._popContext();
  2374. debug.checkForgottenReturns(
  2375. value, promiseCreated, "Promise.try", ret);
  2376. ret._resolveFromSyncValue(value);
  2377. return ret;
  2378. };
  2379. Promise.prototype._resolveFromSyncValue = function (value) {
  2380. if (value === util.errorObj) {
  2381. this._rejectCallback(value.e, false);
  2382. } else {
  2383. this._resolveCallback(value, true);
  2384. }
  2385. };
  2386. };
  2387. },{"./util":36}],20:[function(_dereq_,module,exports){
  2388. "use strict";
  2389. var util = _dereq_("./util");
  2390. var maybeWrapAsError = util.maybeWrapAsError;
  2391. var errors = _dereq_("./errors");
  2392. var OperationalError = errors.OperationalError;
  2393. var es5 = _dereq_("./es5");
  2394. function isUntypedError(obj) {
  2395. return obj instanceof Error &&
  2396. es5.getPrototypeOf(obj) === Error.prototype;
  2397. }
  2398. var rErrorKey = /^(?:name|message|stack|cause)$/;
  2399. function wrapAsOperationalError(obj) {
  2400. var ret;
  2401. if (isUntypedError(obj)) {
  2402. ret = new OperationalError(obj);
  2403. ret.name = obj.name;
  2404. ret.message = obj.message;
  2405. ret.stack = obj.stack;
  2406. var keys = es5.keys(obj);
  2407. for (var i = 0; i < keys.length; ++i) {
  2408. var key = keys[i];
  2409. if (!rErrorKey.test(key)) {
  2410. ret[key] = obj[key];
  2411. }
  2412. }
  2413. return ret;
  2414. }
  2415. util.markAsOriginatingFromRejection(obj);
  2416. return obj;
  2417. }
  2418. function nodebackForPromise(promise, multiArgs) {
  2419. return function(err, value) {
  2420. if (promise === null) return;
  2421. if (err) {
  2422. var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
  2423. promise._attachExtraTrace(wrapped);
  2424. promise._reject(wrapped);
  2425. } else if (!multiArgs) {
  2426. promise._fulfill(value);
  2427. } else {
  2428. var args = [].slice.call(arguments, 1);;
  2429. promise._fulfill(args);
  2430. }
  2431. promise = null;
  2432. };
  2433. }
  2434. module.exports = nodebackForPromise;
  2435. },{"./errors":12,"./es5":13,"./util":36}],21:[function(_dereq_,module,exports){
  2436. "use strict";
  2437. module.exports = function(Promise) {
  2438. var util = _dereq_("./util");
  2439. var async = Promise._async;
  2440. var tryCatch = util.tryCatch;
  2441. var errorObj = util.errorObj;
  2442. function spreadAdapter(val, nodeback) {
  2443. var promise = this;
  2444. if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);
  2445. var ret =
  2446. tryCatch(nodeback).apply(promise._boundValue(), [null].concat(val));
  2447. if (ret === errorObj) {
  2448. async.throwLater(ret.e);
  2449. }
  2450. }
  2451. function successAdapter(val, nodeback) {
  2452. var promise = this;
  2453. var receiver = promise._boundValue();
  2454. var ret = val === undefined
  2455. ? tryCatch(nodeback).call(receiver, null)
  2456. : tryCatch(nodeback).call(receiver, null, val);
  2457. if (ret === errorObj) {
  2458. async.throwLater(ret.e);
  2459. }
  2460. }
  2461. function errorAdapter(reason, nodeback) {
  2462. var promise = this;
  2463. if (!reason) {
  2464. var newReason = new Error(reason + "");
  2465. newReason.cause = reason;
  2466. reason = newReason;
  2467. }
  2468. var ret = tryCatch(nodeback).call(promise._boundValue(), reason);
  2469. if (ret === errorObj) {
  2470. async.throwLater(ret.e);
  2471. }
  2472. }
  2473. Promise.prototype.asCallback = Promise.prototype.nodeify = function (nodeback,
  2474. options) {
  2475. if (typeof nodeback == "function") {
  2476. var adapter = successAdapter;
  2477. if (options !== undefined && Object(options).spread) {
  2478. adapter = spreadAdapter;
  2479. }
  2480. this._then(
  2481. adapter,
  2482. errorAdapter,
  2483. undefined,
  2484. this,
  2485. nodeback
  2486. );
  2487. }
  2488. return this;
  2489. };
  2490. };
  2491. },{"./util":36}],22:[function(_dereq_,module,exports){
  2492. "use strict";
  2493. module.exports = function() {
  2494. var makeSelfResolutionError = function () {
  2495. return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2496. };
  2497. var reflectHandler = function() {
  2498. return new Promise.PromiseInspection(this._target());
  2499. };
  2500. var apiRejection = function(msg) {
  2501. return Promise.reject(new TypeError(msg));
  2502. };
  2503. function Proxyable() {}
  2504. var UNDEFINED_BINDING = {};
  2505. var util = _dereq_("./util");
  2506. var getDomain;
  2507. if (util.isNode) {
  2508. getDomain = function() {
  2509. var ret = process.domain;
  2510. if (ret === undefined) ret = null;
  2511. return ret;
  2512. };
  2513. } else {
  2514. getDomain = function() {
  2515. return null;
  2516. };
  2517. }
  2518. util.notEnumerableProp(Promise, "_getDomain", getDomain);
  2519. var es5 = _dereq_("./es5");
  2520. var Async = _dereq_("./async");
  2521. var async = new Async();
  2522. es5.defineProperty(Promise, "_async", {value: async});
  2523. var errors = _dereq_("./errors");
  2524. var TypeError = Promise.TypeError = errors.TypeError;
  2525. Promise.RangeError = errors.RangeError;
  2526. var CancellationError = Promise.CancellationError = errors.CancellationError;
  2527. Promise.TimeoutError = errors.TimeoutError;
  2528. Promise.OperationalError = errors.OperationalError;
  2529. Promise.RejectionError = errors.OperationalError;
  2530. Promise.AggregateError = errors.AggregateError;
  2531. var INTERNAL = function(){};
  2532. var APPLY = {};
  2533. var NEXT_FILTER = {};
  2534. var tryConvertToPromise = _dereq_("./thenables")(Promise, INTERNAL);
  2535. var PromiseArray =
  2536. _dereq_("./promise_array")(Promise, INTERNAL,
  2537. tryConvertToPromise, apiRejection, Proxyable);
  2538. var Context = _dereq_("./context")(Promise);
  2539. /*jshint unused:false*/
  2540. var createContext = Context.create;
  2541. var debug = _dereq_("./debuggability")(Promise, Context);
  2542. var CapturedTrace = debug.CapturedTrace;
  2543. var PassThroughHandlerContext =
  2544. _dereq_("./finally")(Promise, tryConvertToPromise, NEXT_FILTER);
  2545. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  2546. var nodebackForPromise = _dereq_("./nodeback");
  2547. var errorObj = util.errorObj;
  2548. var tryCatch = util.tryCatch;
  2549. function check(self, executor) {
  2550. if (self == null || self.constructor !== Promise) {
  2551. throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2552. }
  2553. if (typeof executor !== "function") {
  2554. throw new TypeError("expecting a function but got " + util.classString(executor));
  2555. }
  2556. }
  2557. function Promise(executor) {
  2558. if (executor !== INTERNAL) {
  2559. check(this, executor);
  2560. }
  2561. this._bitField = 0;
  2562. this._fulfillmentHandler0 = undefined;
  2563. this._rejectionHandler0 = undefined;
  2564. this._promise0 = undefined;
  2565. this._receiver0 = undefined;
  2566. this._resolveFromExecutor(executor);
  2567. this._promiseCreated();
  2568. this._fireEvent("promiseCreated", this);
  2569. }
  2570. Promise.prototype.toString = function () {
  2571. return "[object Promise]";
  2572. };
  2573. Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
  2574. var len = arguments.length;
  2575. if (len > 1) {
  2576. var catchInstances = new Array(len - 1),
  2577. j = 0, i;
  2578. for (i = 0; i < len - 1; ++i) {
  2579. var item = arguments[i];
  2580. if (util.isObject(item)) {
  2581. catchInstances[j++] = item;
  2582. } else {
  2583. return apiRejection("Catch statement predicate: " +
  2584. "expecting an object but got " + util.classString(item));
  2585. }
  2586. }
  2587. catchInstances.length = j;
  2588. fn = arguments[i];
  2589. return this.then(undefined, catchFilter(catchInstances, fn, this));
  2590. }
  2591. return this.then(undefined, fn);
  2592. };
  2593. Promise.prototype.reflect = function () {
  2594. return this._then(reflectHandler,
  2595. reflectHandler, undefined, this, undefined);
  2596. };
  2597. Promise.prototype.then = function (didFulfill, didReject) {
  2598. if (debug.warnings() && arguments.length > 0 &&
  2599. typeof didFulfill !== "function" &&
  2600. typeof didReject !== "function") {
  2601. var msg = ".then() only accepts functions but was passed: " +
  2602. util.classString(didFulfill);
  2603. if (arguments.length > 1) {
  2604. msg += ", " + util.classString(didReject);
  2605. }
  2606. this._warn(msg);
  2607. }
  2608. return this._then(didFulfill, didReject, undefined, undefined, undefined);
  2609. };
  2610. Promise.prototype.done = function (didFulfill, didReject) {
  2611. var promise =
  2612. this._then(didFulfill, didReject, undefined, undefined, undefined);
  2613. promise._setIsFinal();
  2614. };
  2615. Promise.prototype.spread = function (fn) {
  2616. if (typeof fn !== "function") {
  2617. return apiRejection("expecting a function but got " + util.classString(fn));
  2618. }
  2619. return this.all()._then(fn, undefined, undefined, APPLY, undefined);
  2620. };
  2621. Promise.prototype.toJSON = function () {
  2622. var ret = {
  2623. isFulfilled: false,
  2624. isRejected: false,
  2625. fulfillmentValue: undefined,
  2626. rejectionReason: undefined
  2627. };
  2628. if (this.isFulfilled()) {
  2629. ret.fulfillmentValue = this.value();
  2630. ret.isFulfilled = true;
  2631. } else if (this.isRejected()) {
  2632. ret.rejectionReason = this.reason();
  2633. ret.isRejected = true;
  2634. }
  2635. return ret;
  2636. };
  2637. Promise.prototype.all = function () {
  2638. if (arguments.length > 0) {
  2639. this._warn(".all() was passed arguments but it does not take any");
  2640. }
  2641. return new PromiseArray(this).promise();
  2642. };
  2643. Promise.prototype.error = function (fn) {
  2644. return this.caught(util.originatesFromRejection, fn);
  2645. };
  2646. Promise.getNewLibraryCopy = module.exports;
  2647. Promise.is = function (val) {
  2648. return val instanceof Promise;
  2649. };
  2650. Promise.fromNode = Promise.fromCallback = function(fn) {
  2651. var ret = new Promise(INTERNAL);
  2652. ret._captureStackTrace();
  2653. var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
  2654. : false;
  2655. var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
  2656. if (result === errorObj) {
  2657. ret._rejectCallback(result.e, true);
  2658. }
  2659. if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
  2660. return ret;
  2661. };
  2662. Promise.all = function (promises) {
  2663. return new PromiseArray(promises).promise();
  2664. };
  2665. Promise.cast = function (obj) {
  2666. var ret = tryConvertToPromise(obj);
  2667. if (!(ret instanceof Promise)) {
  2668. ret = new Promise(INTERNAL);
  2669. ret._captureStackTrace();
  2670. ret._setFulfilled();
  2671. ret._rejectionHandler0 = obj;
  2672. }
  2673. return ret;
  2674. };
  2675. Promise.resolve = Promise.fulfilled = Promise.cast;
  2676. Promise.reject = Promise.rejected = function (reason) {
  2677. var ret = new Promise(INTERNAL);
  2678. ret._captureStackTrace();
  2679. ret._rejectCallback(reason, true);
  2680. return ret;
  2681. };
  2682. Promise.setScheduler = function(fn) {
  2683. if (typeof fn !== "function") {
  2684. throw new TypeError("expecting a function but got " + util.classString(fn));
  2685. }
  2686. return async.setScheduler(fn);
  2687. };
  2688. Promise.prototype._then = function (
  2689. didFulfill,
  2690. didReject,
  2691. _, receiver,
  2692. internalData
  2693. ) {
  2694. var haveInternalData = internalData !== undefined;
  2695. var promise = haveInternalData ? internalData : new Promise(INTERNAL);
  2696. var target = this._target();
  2697. var bitField = target._bitField;
  2698. if (!haveInternalData) {
  2699. promise._propagateFrom(this, 3);
  2700. promise._captureStackTrace();
  2701. if (receiver === undefined &&
  2702. ((this._bitField & 2097152) !== 0)) {
  2703. if (!((bitField & 50397184) === 0)) {
  2704. receiver = this._boundValue();
  2705. } else {
  2706. receiver = target === this ? undefined : this._boundTo;
  2707. }
  2708. }
  2709. this._fireEvent("promiseChained", this, promise);
  2710. }
  2711. var domain = getDomain();
  2712. if (!((bitField & 50397184) === 0)) {
  2713. var handler, value, settler = target._settlePromiseCtx;
  2714. if (((bitField & 33554432) !== 0)) {
  2715. value = target._rejectionHandler0;
  2716. handler = didFulfill;
  2717. } else if (((bitField & 16777216) !== 0)) {
  2718. value = target._fulfillmentHandler0;
  2719. handler = didReject;
  2720. target._unsetRejectionIsUnhandled();
  2721. } else {
  2722. settler = target._settlePromiseLateCancellationObserver;
  2723. value = new CancellationError("late cancellation observer");
  2724. target._attachExtraTrace(value);
  2725. handler = didReject;
  2726. }
  2727. async.invoke(settler, target, {
  2728. handler: domain === null ? handler
  2729. : (typeof handler === "function" &&
  2730. util.domainBind(domain, handler)),
  2731. promise: promise,
  2732. receiver: receiver,
  2733. value: value
  2734. });
  2735. } else {
  2736. target._addCallbacks(didFulfill, didReject, promise, receiver, domain);
  2737. }
  2738. return promise;
  2739. };
  2740. Promise.prototype._length = function () {
  2741. return this._bitField & 65535;
  2742. };
  2743. Promise.prototype._isFateSealed = function () {
  2744. return (this._bitField & 117506048) !== 0;
  2745. };
  2746. Promise.prototype._isFollowing = function () {
  2747. return (this._bitField & 67108864) === 67108864;
  2748. };
  2749. Promise.prototype._setLength = function (len) {
  2750. this._bitField = (this._bitField & -65536) |
  2751. (len & 65535);
  2752. };
  2753. Promise.prototype._setFulfilled = function () {
  2754. this._bitField = this._bitField | 33554432;
  2755. this._fireEvent("promiseFulfilled", this);
  2756. };
  2757. Promise.prototype._setRejected = function () {
  2758. this._bitField = this._bitField | 16777216;
  2759. this._fireEvent("promiseRejected", this);
  2760. };
  2761. Promise.prototype._setFollowing = function () {
  2762. this._bitField = this._bitField | 67108864;
  2763. this._fireEvent("promiseResolved", this);
  2764. };
  2765. Promise.prototype._setIsFinal = function () {
  2766. this._bitField = this._bitField | 4194304;
  2767. };
  2768. Promise.prototype._isFinal = function () {
  2769. return (this._bitField & 4194304) > 0;
  2770. };
  2771. Promise.prototype._unsetCancelled = function() {
  2772. this._bitField = this._bitField & (~65536);
  2773. };
  2774. Promise.prototype._setCancelled = function() {
  2775. this._bitField = this._bitField | 65536;
  2776. this._fireEvent("promiseCancelled", this);
  2777. };
  2778. Promise.prototype._setWillBeCancelled = function() {
  2779. this._bitField = this._bitField | 8388608;
  2780. };
  2781. Promise.prototype._setAsyncGuaranteed = function() {
  2782. if (async.hasCustomScheduler()) return;
  2783. this._bitField = this._bitField | 134217728;
  2784. };
  2785. Promise.prototype._receiverAt = function (index) {
  2786. var ret = index === 0 ? this._receiver0 : this[
  2787. index * 4 - 4 + 3];
  2788. if (ret === UNDEFINED_BINDING) {
  2789. return undefined;
  2790. } else if (ret === undefined && this._isBound()) {
  2791. return this._boundValue();
  2792. }
  2793. return ret;
  2794. };
  2795. Promise.prototype._promiseAt = function (index) {
  2796. return this[
  2797. index * 4 - 4 + 2];
  2798. };
  2799. Promise.prototype._fulfillmentHandlerAt = function (index) {
  2800. return this[
  2801. index * 4 - 4 + 0];
  2802. };
  2803. Promise.prototype._rejectionHandlerAt = function (index) {
  2804. return this[
  2805. index * 4 - 4 + 1];
  2806. };
  2807. Promise.prototype._boundValue = function() {};
  2808. Promise.prototype._migrateCallback0 = function (follower) {
  2809. var bitField = follower._bitField;
  2810. var fulfill = follower._fulfillmentHandler0;
  2811. var reject = follower._rejectionHandler0;
  2812. var promise = follower._promise0;
  2813. var receiver = follower._receiverAt(0);
  2814. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2815. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2816. };
  2817. Promise.prototype._migrateCallbackAt = function (follower, index) {
  2818. var fulfill = follower._fulfillmentHandlerAt(index);
  2819. var reject = follower._rejectionHandlerAt(index);
  2820. var promise = follower._promiseAt(index);
  2821. var receiver = follower._receiverAt(index);
  2822. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2823. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2824. };
  2825. Promise.prototype._addCallbacks = function (
  2826. fulfill,
  2827. reject,
  2828. promise,
  2829. receiver,
  2830. domain
  2831. ) {
  2832. var index = this._length();
  2833. if (index >= 65535 - 4) {
  2834. index = 0;
  2835. this._setLength(0);
  2836. }
  2837. if (index === 0) {
  2838. this._promise0 = promise;
  2839. this._receiver0 = receiver;
  2840. if (typeof fulfill === "function") {
  2841. this._fulfillmentHandler0 =
  2842. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2843. }
  2844. if (typeof reject === "function") {
  2845. this._rejectionHandler0 =
  2846. domain === null ? reject : util.domainBind(domain, reject);
  2847. }
  2848. } else {
  2849. var base = index * 4 - 4;
  2850. this[base + 2] = promise;
  2851. this[base + 3] = receiver;
  2852. if (typeof fulfill === "function") {
  2853. this[base + 0] =
  2854. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2855. }
  2856. if (typeof reject === "function") {
  2857. this[base + 1] =
  2858. domain === null ? reject : util.domainBind(domain, reject);
  2859. }
  2860. }
  2861. this._setLength(index + 1);
  2862. return index;
  2863. };
  2864. Promise.prototype._proxy = function (proxyable, arg) {
  2865. this._addCallbacks(undefined, undefined, arg, proxyable, null);
  2866. };
  2867. Promise.prototype._resolveCallback = function(value, shouldBind) {
  2868. if (((this._bitField & 117506048) !== 0)) return;
  2869. if (value === this)
  2870. return this._rejectCallback(makeSelfResolutionError(), false);
  2871. var maybePromise = tryConvertToPromise(value, this);
  2872. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  2873. if (shouldBind) this._propagateFrom(maybePromise, 2);
  2874. var promise = maybePromise._target();
  2875. if (promise === this) {
  2876. this._reject(makeSelfResolutionError());
  2877. return;
  2878. }
  2879. var bitField = promise._bitField;
  2880. if (((bitField & 50397184) === 0)) {
  2881. var len = this._length();
  2882. if (len > 0) promise._migrateCallback0(this);
  2883. for (var i = 1; i < len; ++i) {
  2884. promise._migrateCallbackAt(this, i);
  2885. }
  2886. this._setFollowing();
  2887. this._setLength(0);
  2888. this._setFollowee(promise);
  2889. } else if (((bitField & 33554432) !== 0)) {
  2890. this._fulfill(promise._value());
  2891. } else if (((bitField & 16777216) !== 0)) {
  2892. this._reject(promise._reason());
  2893. } else {
  2894. var reason = new CancellationError("late cancellation observer");
  2895. promise._attachExtraTrace(reason);
  2896. this._reject(reason);
  2897. }
  2898. };
  2899. Promise.prototype._rejectCallback =
  2900. function(reason, synchronous, ignoreNonErrorWarnings) {
  2901. var trace = util.ensureErrorObject(reason);
  2902. var hasStack = trace === reason;
  2903. if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
  2904. var message = "a promise was rejected with a non-error: " +
  2905. util.classString(reason);
  2906. this._warn(message, true);
  2907. }
  2908. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  2909. this._reject(reason);
  2910. };
  2911. Promise.prototype._resolveFromExecutor = function (executor) {
  2912. if (executor === INTERNAL) return;
  2913. var promise = this;
  2914. this._captureStackTrace();
  2915. this._pushContext();
  2916. var synchronous = true;
  2917. var r = this._execute(executor, function(value) {
  2918. promise._resolveCallback(value);
  2919. }, function (reason) {
  2920. promise._rejectCallback(reason, synchronous);
  2921. });
  2922. synchronous = false;
  2923. this._popContext();
  2924. if (r !== undefined) {
  2925. promise._rejectCallback(r, true);
  2926. }
  2927. };
  2928. Promise.prototype._settlePromiseFromHandler = function (
  2929. handler, receiver, value, promise
  2930. ) {
  2931. var bitField = promise._bitField;
  2932. if (((bitField & 65536) !== 0)) return;
  2933. promise._pushContext();
  2934. var x;
  2935. if (receiver === APPLY) {
  2936. if (!value || typeof value.length !== "number") {
  2937. x = errorObj;
  2938. x.e = new TypeError("cannot .spread() a non-array: " +
  2939. util.classString(value));
  2940. } else {
  2941. x = tryCatch(handler).apply(this._boundValue(), value);
  2942. }
  2943. } else {
  2944. x = tryCatch(handler).call(receiver, value);
  2945. }
  2946. var promiseCreated = promise._popContext();
  2947. bitField = promise._bitField;
  2948. if (((bitField & 65536) !== 0)) return;
  2949. if (x === NEXT_FILTER) {
  2950. promise._reject(value);
  2951. } else if (x === errorObj) {
  2952. promise._rejectCallback(x.e, false);
  2953. } else {
  2954. debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
  2955. promise._resolveCallback(x);
  2956. }
  2957. };
  2958. Promise.prototype._target = function() {
  2959. var ret = this;
  2960. while (ret._isFollowing()) ret = ret._followee();
  2961. return ret;
  2962. };
  2963. Promise.prototype._followee = function() {
  2964. return this._rejectionHandler0;
  2965. };
  2966. Promise.prototype._setFollowee = function(promise) {
  2967. this._rejectionHandler0 = promise;
  2968. };
  2969. Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
  2970. var isPromise = promise instanceof Promise;
  2971. var bitField = this._bitField;
  2972. var asyncGuaranteed = ((bitField & 134217728) !== 0);
  2973. if (((bitField & 65536) !== 0)) {
  2974. if (isPromise) promise._invokeInternalOnCancel();
  2975. if (receiver instanceof PassThroughHandlerContext &&
  2976. receiver.isFinallyHandler()) {
  2977. receiver.cancelPromise = promise;
  2978. if (tryCatch(handler).call(receiver, value) === errorObj) {
  2979. promise._reject(errorObj.e);
  2980. }
  2981. } else if (handler === reflectHandler) {
  2982. promise._fulfill(reflectHandler.call(receiver));
  2983. } else if (receiver instanceof Proxyable) {
  2984. receiver._promiseCancelled(promise);
  2985. } else if (isPromise || promise instanceof PromiseArray) {
  2986. promise._cancel();
  2987. } else {
  2988. receiver.cancel();
  2989. }
  2990. } else if (typeof handler === "function") {
  2991. if (!isPromise) {
  2992. handler.call(receiver, value, promise);
  2993. } else {
  2994. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2995. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2996. }
  2997. } else if (receiver instanceof Proxyable) {
  2998. if (!receiver._isResolved()) {
  2999. if (((bitField & 33554432) !== 0)) {
  3000. receiver._promiseFulfilled(value, promise);
  3001. } else {
  3002. receiver._promiseRejected(value, promise);
  3003. }
  3004. }
  3005. } else if (isPromise) {
  3006. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  3007. if (((bitField & 33554432) !== 0)) {
  3008. promise._fulfill(value);
  3009. } else {
  3010. promise._reject(value);
  3011. }
  3012. }
  3013. };
  3014. Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
  3015. var handler = ctx.handler;
  3016. var promise = ctx.promise;
  3017. var receiver = ctx.receiver;
  3018. var value = ctx.value;
  3019. if (typeof handler === "function") {
  3020. if (!(promise instanceof Promise)) {
  3021. handler.call(receiver, value, promise);
  3022. } else {
  3023. this._settlePromiseFromHandler(handler, receiver, value, promise);
  3024. }
  3025. } else if (promise instanceof Promise) {
  3026. promise._reject(value);
  3027. }
  3028. };
  3029. Promise.prototype._settlePromiseCtx = function(ctx) {
  3030. this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
  3031. };
  3032. Promise.prototype._settlePromise0 = function(handler, value, bitField) {
  3033. var promise = this._promise0;
  3034. var receiver = this._receiverAt(0);
  3035. this._promise0 = undefined;
  3036. this._receiver0 = undefined;
  3037. this._settlePromise(promise, handler, receiver, value);
  3038. };
  3039. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  3040. var base = index * 4 - 4;
  3041. this[base + 2] =
  3042. this[base + 3] =
  3043. this[base + 0] =
  3044. this[base + 1] = undefined;
  3045. };
  3046. Promise.prototype._fulfill = function (value) {
  3047. var bitField = this._bitField;
  3048. if (((bitField & 117506048) >>> 16)) return;
  3049. if (value === this) {
  3050. var err = makeSelfResolutionError();
  3051. this._attachExtraTrace(err);
  3052. return this._reject(err);
  3053. }
  3054. this._setFulfilled();
  3055. this._rejectionHandler0 = value;
  3056. if ((bitField & 65535) > 0) {
  3057. if (((bitField & 134217728) !== 0)) {
  3058. this._settlePromises();
  3059. } else {
  3060. async.settlePromises(this);
  3061. }
  3062. }
  3063. };
  3064. Promise.prototype._reject = function (reason) {
  3065. var bitField = this._bitField;
  3066. if (((bitField & 117506048) >>> 16)) return;
  3067. this._setRejected();
  3068. this._fulfillmentHandler0 = reason;
  3069. if (this._isFinal()) {
  3070. return async.fatalError(reason, util.isNode);
  3071. }
  3072. if ((bitField & 65535) > 0) {
  3073. async.settlePromises(this);
  3074. } else {
  3075. this._ensurePossibleRejectionHandled();
  3076. }
  3077. };
  3078. Promise.prototype._fulfillPromises = function (len, value) {
  3079. for (var i = 1; i < len; i++) {
  3080. var handler = this._fulfillmentHandlerAt(i);
  3081. var promise = this._promiseAt(i);
  3082. var receiver = this._receiverAt(i);
  3083. this._clearCallbackDataAtIndex(i);
  3084. this._settlePromise(promise, handler, receiver, value);
  3085. }
  3086. };
  3087. Promise.prototype._rejectPromises = function (len, reason) {
  3088. for (var i = 1; i < len; i++) {
  3089. var handler = this._rejectionHandlerAt(i);
  3090. var promise = this._promiseAt(i);
  3091. var receiver = this._receiverAt(i);
  3092. this._clearCallbackDataAtIndex(i);
  3093. this._settlePromise(promise, handler, receiver, reason);
  3094. }
  3095. };
  3096. Promise.prototype._settlePromises = function () {
  3097. var bitField = this._bitField;
  3098. var len = (bitField & 65535);
  3099. if (len > 0) {
  3100. if (((bitField & 16842752) !== 0)) {
  3101. var reason = this._fulfillmentHandler0;
  3102. this._settlePromise0(this._rejectionHandler0, reason, bitField);
  3103. this._rejectPromises(len, reason);
  3104. } else {
  3105. var value = this._rejectionHandler0;
  3106. this._settlePromise0(this._fulfillmentHandler0, value, bitField);
  3107. this._fulfillPromises(len, value);
  3108. }
  3109. this._setLength(0);
  3110. }
  3111. this._clearCancellationData();
  3112. };
  3113. Promise.prototype._settledValue = function() {
  3114. var bitField = this._bitField;
  3115. if (((bitField & 33554432) !== 0)) {
  3116. return this._rejectionHandler0;
  3117. } else if (((bitField & 16777216) !== 0)) {
  3118. return this._fulfillmentHandler0;
  3119. }
  3120. };
  3121. function deferResolve(v) {this.promise._resolveCallback(v);}
  3122. function deferReject(v) {this.promise._rejectCallback(v, false);}
  3123. Promise.defer = Promise.pending = function() {
  3124. debug.deprecated("Promise.defer", "new Promise");
  3125. var promise = new Promise(INTERNAL);
  3126. return {
  3127. promise: promise,
  3128. resolve: deferResolve,
  3129. reject: deferReject
  3130. };
  3131. };
  3132. util.notEnumerableProp(Promise,
  3133. "_makeSelfResolutionError",
  3134. makeSelfResolutionError);
  3135. _dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
  3136. debug);
  3137. _dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
  3138. _dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug);
  3139. _dereq_("./direct_resolve")(Promise);
  3140. _dereq_("./synchronous_inspection")(Promise);
  3141. _dereq_("./join")(
  3142. Promise, PromiseArray, tryConvertToPromise, INTERNAL, async, getDomain);
  3143. Promise.Promise = Promise;
  3144. Promise.version = "3.5.1";
  3145. _dereq_('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
  3146. _dereq_('./call_get.js')(Promise);
  3147. _dereq_('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext, INTERNAL, debug);
  3148. _dereq_('./timers.js')(Promise, INTERNAL, debug);
  3149. _dereq_('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise, Proxyable, debug);
  3150. _dereq_('./nodeify.js')(Promise);
  3151. _dereq_('./promisify.js')(Promise, INTERNAL);
  3152. _dereq_('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
  3153. _dereq_('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
  3154. _dereq_('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
  3155. _dereq_('./settle.js')(Promise, PromiseArray, debug);
  3156. _dereq_('./some.js')(Promise, PromiseArray, apiRejection);
  3157. _dereq_('./filter.js')(Promise, INTERNAL);
  3158. _dereq_('./each.js')(Promise, INTERNAL);
  3159. _dereq_('./any.js')(Promise);
  3160. util.toFastProperties(Promise);
  3161. util.toFastProperties(Promise.prototype);
  3162. function fillTypes(value) {
  3163. var p = new Promise(INTERNAL);
  3164. p._fulfillmentHandler0 = value;
  3165. p._rejectionHandler0 = value;
  3166. p._promise0 = value;
  3167. p._receiver0 = value;
  3168. }
  3169. // Complete slack tracking, opt out of field-type tracking and
  3170. // stabilize map
  3171. fillTypes({a: 1});
  3172. fillTypes({b: 2});
  3173. fillTypes({c: 3});
  3174. fillTypes(1);
  3175. fillTypes(function(){});
  3176. fillTypes(undefined);
  3177. fillTypes(false);
  3178. fillTypes(new Promise(INTERNAL));
  3179. debug.setBounds(Async.firstLineError, util.lastLineError);
  3180. return Promise;
  3181. };
  3182. },{"./any.js":1,"./async":2,"./bind":3,"./call_get.js":5,"./cancel":6,"./catch_filter":7,"./context":8,"./debuggability":9,"./direct_resolve":10,"./each.js":11,"./errors":12,"./es5":13,"./filter.js":14,"./finally":15,"./generators.js":16,"./join":17,"./map.js":18,"./method":19,"./nodeback":20,"./nodeify.js":21,"./promise_array":23,"./promisify.js":24,"./props.js":25,"./race.js":27,"./reduce.js":28,"./settle.js":30,"./some.js":31,"./synchronous_inspection":32,"./thenables":33,"./timers.js":34,"./using.js":35,"./util":36}],23:[function(_dereq_,module,exports){
  3183. "use strict";
  3184. module.exports = function(Promise, INTERNAL, tryConvertToPromise,
  3185. apiRejection, Proxyable) {
  3186. var util = _dereq_("./util");
  3187. var isArray = util.isArray;
  3188. function toResolutionValue(val) {
  3189. switch(val) {
  3190. case -2: return [];
  3191. case -3: return {};
  3192. case -6: return new Map();
  3193. }
  3194. }
  3195. function PromiseArray(values) {
  3196. var promise = this._promise = new Promise(INTERNAL);
  3197. if (values instanceof Promise) {
  3198. promise._propagateFrom(values, 3);
  3199. }
  3200. promise._setOnCancel(this);
  3201. this._values = values;
  3202. this._length = 0;
  3203. this._totalResolved = 0;
  3204. this._init(undefined, -2);
  3205. }
  3206. util.inherits(PromiseArray, Proxyable);
  3207. PromiseArray.prototype.length = function () {
  3208. return this._length;
  3209. };
  3210. PromiseArray.prototype.promise = function () {
  3211. return this._promise;
  3212. };
  3213. PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
  3214. var values = tryConvertToPromise(this._values, this._promise);
  3215. if (values instanceof Promise) {
  3216. values = values._target();
  3217. var bitField = values._bitField;
  3218. ;
  3219. this._values = values;
  3220. if (((bitField & 50397184) === 0)) {
  3221. this._promise._setAsyncGuaranteed();
  3222. return values._then(
  3223. init,
  3224. this._reject,
  3225. undefined,
  3226. this,
  3227. resolveValueIfEmpty
  3228. );
  3229. } else if (((bitField & 33554432) !== 0)) {
  3230. values = values._value();
  3231. } else if (((bitField & 16777216) !== 0)) {
  3232. return this._reject(values._reason());
  3233. } else {
  3234. return this._cancel();
  3235. }
  3236. }
  3237. values = util.asArray(values);
  3238. if (values === null) {
  3239. var err = apiRejection(
  3240. "expecting an array or an iterable object but got " + util.classString(values)).reason();
  3241. this._promise._rejectCallback(err, false);
  3242. return;
  3243. }
  3244. if (values.length === 0) {
  3245. if (resolveValueIfEmpty === -5) {
  3246. this._resolveEmptyArray();
  3247. }
  3248. else {
  3249. this._resolve(toResolutionValue(resolveValueIfEmpty));
  3250. }
  3251. return;
  3252. }
  3253. this._iterate(values);
  3254. };
  3255. PromiseArray.prototype._iterate = function(values) {
  3256. var len = this.getActualLength(values.length);
  3257. this._length = len;
  3258. this._values = this.shouldCopyValues() ? new Array(len) : this._values;
  3259. var result = this._promise;
  3260. var isResolved = false;
  3261. var bitField = null;
  3262. for (var i = 0; i < len; ++i) {
  3263. var maybePromise = tryConvertToPromise(values[i], result);
  3264. if (maybePromise instanceof Promise) {
  3265. maybePromise = maybePromise._target();
  3266. bitField = maybePromise._bitField;
  3267. } else {
  3268. bitField = null;
  3269. }
  3270. if (isResolved) {
  3271. if (bitField !== null) {
  3272. maybePromise.suppressUnhandledRejections();
  3273. }
  3274. } else if (bitField !== null) {
  3275. if (((bitField & 50397184) === 0)) {
  3276. maybePromise._proxy(this, i);
  3277. this._values[i] = maybePromise;
  3278. } else if (((bitField & 33554432) !== 0)) {
  3279. isResolved = this._promiseFulfilled(maybePromise._value(), i);
  3280. } else if (((bitField & 16777216) !== 0)) {
  3281. isResolved = this._promiseRejected(maybePromise._reason(), i);
  3282. } else {
  3283. isResolved = this._promiseCancelled(i);
  3284. }
  3285. } else {
  3286. isResolved = this._promiseFulfilled(maybePromise, i);
  3287. }
  3288. }
  3289. if (!isResolved) result._setAsyncGuaranteed();
  3290. };
  3291. PromiseArray.prototype._isResolved = function () {
  3292. return this._values === null;
  3293. };
  3294. PromiseArray.prototype._resolve = function (value) {
  3295. this._values = null;
  3296. this._promise._fulfill(value);
  3297. };
  3298. PromiseArray.prototype._cancel = function() {
  3299. if (this._isResolved() || !this._promise._isCancellable()) return;
  3300. this._values = null;
  3301. this._promise._cancel();
  3302. };
  3303. PromiseArray.prototype._reject = function (reason) {
  3304. this._values = null;
  3305. this._promise._rejectCallback(reason, false);
  3306. };
  3307. PromiseArray.prototype._promiseFulfilled = function (value, index) {
  3308. this._values[index] = value;
  3309. var totalResolved = ++this._totalResolved;
  3310. if (totalResolved >= this._length) {
  3311. this._resolve(this._values);
  3312. return true;
  3313. }
  3314. return false;
  3315. };
  3316. PromiseArray.prototype._promiseCancelled = function() {
  3317. this._cancel();
  3318. return true;
  3319. };
  3320. PromiseArray.prototype._promiseRejected = function (reason) {
  3321. this._totalResolved++;
  3322. this._reject(reason);
  3323. return true;
  3324. };
  3325. PromiseArray.prototype._resultCancelled = function() {
  3326. if (this._isResolved()) return;
  3327. var values = this._values;
  3328. this._cancel();
  3329. if (values instanceof Promise) {
  3330. values.cancel();
  3331. } else {
  3332. for (var i = 0; i < values.length; ++i) {
  3333. if (values[i] instanceof Promise) {
  3334. values[i].cancel();
  3335. }
  3336. }
  3337. }
  3338. };
  3339. PromiseArray.prototype.shouldCopyValues = function () {
  3340. return true;
  3341. };
  3342. PromiseArray.prototype.getActualLength = function (len) {
  3343. return len;
  3344. };
  3345. return PromiseArray;
  3346. };
  3347. },{"./util":36}],24:[function(_dereq_,module,exports){
  3348. "use strict";
  3349. module.exports = function(Promise, INTERNAL) {
  3350. var THIS = {};
  3351. var util = _dereq_("./util");
  3352. var nodebackForPromise = _dereq_("./nodeback");
  3353. var withAppended = util.withAppended;
  3354. var maybeWrapAsError = util.maybeWrapAsError;
  3355. var canEvaluate = util.canEvaluate;
  3356. var TypeError = _dereq_("./errors").TypeError;
  3357. var defaultSuffix = "Async";
  3358. var defaultPromisified = {__isPromisified__: true};
  3359. var noCopyProps = [
  3360. "arity", "length",
  3361. "name",
  3362. "arguments",
  3363. "caller",
  3364. "callee",
  3365. "prototype",
  3366. "__isPromisified__"
  3367. ];
  3368. var noCopyPropsPattern = new RegExp("^(?:" + noCopyProps.join("|") + ")$");
  3369. var defaultFilter = function(name) {
  3370. return util.isIdentifier(name) &&
  3371. name.charAt(0) !== "_" &&
  3372. name !== "constructor";
  3373. };
  3374. function propsFilter(key) {
  3375. return !noCopyPropsPattern.test(key);
  3376. }
  3377. function isPromisified(fn) {
  3378. try {
  3379. return fn.__isPromisified__ === true;
  3380. }
  3381. catch (e) {
  3382. return false;
  3383. }
  3384. }
  3385. function hasPromisified(obj, key, suffix) {
  3386. var val = util.getDataPropertyOrDefault(obj, key + suffix,
  3387. defaultPromisified);
  3388. return val ? isPromisified(val) : false;
  3389. }
  3390. function checkValid(ret, suffix, suffixRegexp) {
  3391. for (var i = 0; i < ret.length; i += 2) {
  3392. var key = ret[i];
  3393. if (suffixRegexp.test(key)) {
  3394. var keyWithoutAsyncSuffix = key.replace(suffixRegexp, "");
  3395. for (var j = 0; j < ret.length; j += 2) {
  3396. if (ret[j] === keyWithoutAsyncSuffix) {
  3397. throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/MqrFmX\u000a"
  3398. .replace("%s", suffix));
  3399. }
  3400. }
  3401. }
  3402. }
  3403. }
  3404. function promisifiableMethods(obj, suffix, suffixRegexp, filter) {
  3405. var keys = util.inheritedDataKeys(obj);
  3406. var ret = [];
  3407. for (var i = 0; i < keys.length; ++i) {
  3408. var key = keys[i];
  3409. var value = obj[key];
  3410. var passesDefaultFilter = filter === defaultFilter
  3411. ? true : defaultFilter(key, value, obj);
  3412. if (typeof value === "function" &&
  3413. !isPromisified(value) &&
  3414. !hasPromisified(obj, key, suffix) &&
  3415. filter(key, value, obj, passesDefaultFilter)) {
  3416. ret.push(key, value);
  3417. }
  3418. }
  3419. checkValid(ret, suffix, suffixRegexp);
  3420. return ret;
  3421. }
  3422. var escapeIdentRegex = function(str) {
  3423. return str.replace(/([$])/, "\\$");
  3424. };
  3425. var makeNodePromisifiedEval;
  3426. if (!true) {
  3427. var switchCaseArgumentOrder = function(likelyArgumentCount) {
  3428. var ret = [likelyArgumentCount];
  3429. var min = Math.max(0, likelyArgumentCount - 1 - 3);
  3430. for(var i = likelyArgumentCount - 1; i >= min; --i) {
  3431. ret.push(i);
  3432. }
  3433. for(var i = likelyArgumentCount + 1; i <= 3; ++i) {
  3434. ret.push(i);
  3435. }
  3436. return ret;
  3437. };
  3438. var argumentSequence = function(argumentCount) {
  3439. return util.filledRange(argumentCount, "_arg", "");
  3440. };
  3441. var parameterDeclaration = function(parameterCount) {
  3442. return util.filledRange(
  3443. Math.max(parameterCount, 3), "_arg", "");
  3444. };
  3445. var parameterCount = function(fn) {
  3446. if (typeof fn.length === "number") {
  3447. return Math.max(Math.min(fn.length, 1023 + 1), 0);
  3448. }
  3449. return 0;
  3450. };
  3451. makeNodePromisifiedEval =
  3452. function(callback, receiver, originalName, fn, _, multiArgs) {
  3453. var newParameterCount = Math.max(0, parameterCount(fn) - 1);
  3454. var argumentOrder = switchCaseArgumentOrder(newParameterCount);
  3455. var shouldProxyThis = typeof callback === "string" || receiver === THIS;
  3456. function generateCallForArgumentCount(count) {
  3457. var args = argumentSequence(count).join(", ");
  3458. var comma = count > 0 ? ", " : "";
  3459. var ret;
  3460. if (shouldProxyThis) {
  3461. ret = "ret = callback.call(this, {{args}}, nodeback); break;\n";
  3462. } else {
  3463. ret = receiver === undefined
  3464. ? "ret = callback({{args}}, nodeback); break;\n"
  3465. : "ret = callback.call(receiver, {{args}}, nodeback); break;\n";
  3466. }
  3467. return ret.replace("{{args}}", args).replace(", ", comma);
  3468. }
  3469. function generateArgumentSwitchCase() {
  3470. var ret = "";
  3471. for (var i = 0; i < argumentOrder.length; ++i) {
  3472. ret += "case " + argumentOrder[i] +":" +
  3473. generateCallForArgumentCount(argumentOrder[i]);
  3474. }
  3475. ret += " \n\
  3476. default: \n\
  3477. var args = new Array(len + 1); \n\
  3478. var i = 0; \n\
  3479. for (var i = 0; i < len; ++i) { \n\
  3480. args[i] = arguments[i]; \n\
  3481. } \n\
  3482. args[i] = nodeback; \n\
  3483. [CodeForCall] \n\
  3484. break; \n\
  3485. ".replace("[CodeForCall]", (shouldProxyThis
  3486. ? "ret = callback.apply(this, args);\n"
  3487. : "ret = callback.apply(receiver, args);\n"));
  3488. return ret;
  3489. }
  3490. var getFunctionCode = typeof callback === "string"
  3491. ? ("this != null ? this['"+callback+"'] : fn")
  3492. : "fn";
  3493. var body = "'use strict'; \n\
  3494. var ret = function (Parameters) { \n\
  3495. 'use strict'; \n\
  3496. var len = arguments.length; \n\
  3497. var promise = new Promise(INTERNAL); \n\
  3498. promise._captureStackTrace(); \n\
  3499. var nodeback = nodebackForPromise(promise, " + multiArgs + "); \n\
  3500. var ret; \n\
  3501. var callback = tryCatch([GetFunctionCode]); \n\
  3502. switch(len) { \n\
  3503. [CodeForSwitchCase] \n\
  3504. } \n\
  3505. if (ret === errorObj) { \n\
  3506. promise._rejectCallback(maybeWrapAsError(ret.e), true, true);\n\
  3507. } \n\
  3508. if (!promise._isFateSealed()) promise._setAsyncGuaranteed(); \n\
  3509. return promise; \n\
  3510. }; \n\
  3511. notEnumerableProp(ret, '__isPromisified__', true); \n\
  3512. return ret; \n\
  3513. ".replace("[CodeForSwitchCase]", generateArgumentSwitchCase())
  3514. .replace("[GetFunctionCode]", getFunctionCode);
  3515. body = body.replace("Parameters", parameterDeclaration(newParameterCount));
  3516. return new Function("Promise",
  3517. "fn",
  3518. "receiver",
  3519. "withAppended",
  3520. "maybeWrapAsError",
  3521. "nodebackForPromise",
  3522. "tryCatch",
  3523. "errorObj",
  3524. "notEnumerableProp",
  3525. "INTERNAL",
  3526. body)(
  3527. Promise,
  3528. fn,
  3529. receiver,
  3530. withAppended,
  3531. maybeWrapAsError,
  3532. nodebackForPromise,
  3533. util.tryCatch,
  3534. util.errorObj,
  3535. util.notEnumerableProp,
  3536. INTERNAL);
  3537. };
  3538. }
  3539. function makeNodePromisifiedClosure(callback, receiver, _, fn, __, multiArgs) {
  3540. var defaultThis = (function() {return this;})();
  3541. var method = callback;
  3542. if (typeof method === "string") {
  3543. callback = fn;
  3544. }
  3545. function promisified() {
  3546. var _receiver = receiver;
  3547. if (receiver === THIS) _receiver = this;
  3548. var promise = new Promise(INTERNAL);
  3549. promise._captureStackTrace();
  3550. var cb = typeof method === "string" && this !== defaultThis
  3551. ? this[method] : callback;
  3552. var fn = nodebackForPromise(promise, multiArgs);
  3553. try {
  3554. cb.apply(_receiver, withAppended(arguments, fn));
  3555. } catch(e) {
  3556. promise._rejectCallback(maybeWrapAsError(e), true, true);
  3557. }
  3558. if (!promise._isFateSealed()) promise._setAsyncGuaranteed();
  3559. return promise;
  3560. }
  3561. util.notEnumerableProp(promisified, "__isPromisified__", true);
  3562. return promisified;
  3563. }
  3564. var makeNodePromisified = canEvaluate
  3565. ? makeNodePromisifiedEval
  3566. : makeNodePromisifiedClosure;
  3567. function promisifyAll(obj, suffix, filter, promisifier, multiArgs) {
  3568. var suffixRegexp = new RegExp(escapeIdentRegex(suffix) + "$");
  3569. var methods =
  3570. promisifiableMethods(obj, suffix, suffixRegexp, filter);
  3571. for (var i = 0, len = methods.length; i < len; i+= 2) {
  3572. var key = methods[i];
  3573. var fn = methods[i+1];
  3574. var promisifiedKey = key + suffix;
  3575. if (promisifier === makeNodePromisified) {
  3576. obj[promisifiedKey] =
  3577. makeNodePromisified(key, THIS, key, fn, suffix, multiArgs);
  3578. } else {
  3579. var promisified = promisifier(fn, function() {
  3580. return makeNodePromisified(key, THIS, key,
  3581. fn, suffix, multiArgs);
  3582. });
  3583. util.notEnumerableProp(promisified, "__isPromisified__", true);
  3584. obj[promisifiedKey] = promisified;
  3585. }
  3586. }
  3587. util.toFastProperties(obj);
  3588. return obj;
  3589. }
  3590. function promisify(callback, receiver, multiArgs) {
  3591. return makeNodePromisified(callback, receiver, undefined,
  3592. callback, null, multiArgs);
  3593. }
  3594. Promise.promisify = function (fn, options) {
  3595. if (typeof fn !== "function") {
  3596. throw new TypeError("expecting a function but got " + util.classString(fn));
  3597. }
  3598. if (isPromisified(fn)) {
  3599. return fn;
  3600. }
  3601. options = Object(options);
  3602. var receiver = options.context === undefined ? THIS : options.context;
  3603. var multiArgs = !!options.multiArgs;
  3604. var ret = promisify(fn, receiver, multiArgs);
  3605. util.copyDescriptors(fn, ret, propsFilter);
  3606. return ret;
  3607. };
  3608. Promise.promisifyAll = function (target, options) {
  3609. if (typeof target !== "function" && typeof target !== "object") {
  3610. throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  3611. }
  3612. options = Object(options);
  3613. var multiArgs = !!options.multiArgs;
  3614. var suffix = options.suffix;
  3615. if (typeof suffix !== "string") suffix = defaultSuffix;
  3616. var filter = options.filter;
  3617. if (typeof filter !== "function") filter = defaultFilter;
  3618. var promisifier = options.promisifier;
  3619. if (typeof promisifier !== "function") promisifier = makeNodePromisified;
  3620. if (!util.isIdentifier(suffix)) {
  3621. throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  3622. }
  3623. var keys = util.inheritedDataKeys(target);
  3624. for (var i = 0; i < keys.length; ++i) {
  3625. var value = target[keys[i]];
  3626. if (keys[i] !== "constructor" &&
  3627. util.isClass(value)) {
  3628. promisifyAll(value.prototype, suffix, filter, promisifier,
  3629. multiArgs);
  3630. promisifyAll(value, suffix, filter, promisifier, multiArgs);
  3631. }
  3632. }
  3633. return promisifyAll(target, suffix, filter, promisifier, multiArgs);
  3634. };
  3635. };
  3636. },{"./errors":12,"./nodeback":20,"./util":36}],25:[function(_dereq_,module,exports){
  3637. "use strict";
  3638. module.exports = function(
  3639. Promise, PromiseArray, tryConvertToPromise, apiRejection) {
  3640. var util = _dereq_("./util");
  3641. var isObject = util.isObject;
  3642. var es5 = _dereq_("./es5");
  3643. var Es6Map;
  3644. if (typeof Map === "function") Es6Map = Map;
  3645. var mapToEntries = (function() {
  3646. var index = 0;
  3647. var size = 0;
  3648. function extractEntry(value, key) {
  3649. this[index] = value;
  3650. this[index + size] = key;
  3651. index++;
  3652. }
  3653. return function mapToEntries(map) {
  3654. size = map.size;
  3655. index = 0;
  3656. var ret = new Array(map.size * 2);
  3657. map.forEach(extractEntry, ret);
  3658. return ret;
  3659. };
  3660. })();
  3661. var entriesToMap = function(entries) {
  3662. var ret = new Es6Map();
  3663. var length = entries.length / 2 | 0;
  3664. for (var i = 0; i < length; ++i) {
  3665. var key = entries[length + i];
  3666. var value = entries[i];
  3667. ret.set(key, value);
  3668. }
  3669. return ret;
  3670. };
  3671. function PropertiesPromiseArray(obj) {
  3672. var isMap = false;
  3673. var entries;
  3674. if (Es6Map !== undefined && obj instanceof Es6Map) {
  3675. entries = mapToEntries(obj);
  3676. isMap = true;
  3677. } else {
  3678. var keys = es5.keys(obj);
  3679. var len = keys.length;
  3680. entries = new Array(len * 2);
  3681. for (var i = 0; i < len; ++i) {
  3682. var key = keys[i];
  3683. entries[i] = obj[key];
  3684. entries[i + len] = key;
  3685. }
  3686. }
  3687. this.constructor$(entries);
  3688. this._isMap = isMap;
  3689. this._init$(undefined, isMap ? -6 : -3);
  3690. }
  3691. util.inherits(PropertiesPromiseArray, PromiseArray);
  3692. PropertiesPromiseArray.prototype._init = function () {};
  3693. PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
  3694. this._values[index] = value;
  3695. var totalResolved = ++this._totalResolved;
  3696. if (totalResolved >= this._length) {
  3697. var val;
  3698. if (this._isMap) {
  3699. val = entriesToMap(this._values);
  3700. } else {
  3701. val = {};
  3702. var keyOffset = this.length();
  3703. for (var i = 0, len = this.length(); i < len; ++i) {
  3704. val[this._values[i + keyOffset]] = this._values[i];
  3705. }
  3706. }
  3707. this._resolve(val);
  3708. return true;
  3709. }
  3710. return false;
  3711. };
  3712. PropertiesPromiseArray.prototype.shouldCopyValues = function () {
  3713. return false;
  3714. };
  3715. PropertiesPromiseArray.prototype.getActualLength = function (len) {
  3716. return len >> 1;
  3717. };
  3718. function props(promises) {
  3719. var ret;
  3720. var castValue = tryConvertToPromise(promises);
  3721. if (!isObject(castValue)) {
  3722. return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  3723. } else if (castValue instanceof Promise) {
  3724. ret = castValue._then(
  3725. Promise.props, undefined, undefined, undefined, undefined);
  3726. } else {
  3727. ret = new PropertiesPromiseArray(castValue).promise();
  3728. }
  3729. if (castValue instanceof Promise) {
  3730. ret._propagateFrom(castValue, 2);
  3731. }
  3732. return ret;
  3733. }
  3734. Promise.prototype.props = function () {
  3735. return props(this);
  3736. };
  3737. Promise.props = function (promises) {
  3738. return props(promises);
  3739. };
  3740. };
  3741. },{"./es5":13,"./util":36}],26:[function(_dereq_,module,exports){
  3742. "use strict";
  3743. function arrayMove(src, srcIndex, dst, dstIndex, len) {
  3744. for (var j = 0; j < len; ++j) {
  3745. dst[j + dstIndex] = src[j + srcIndex];
  3746. src[j + srcIndex] = void 0;
  3747. }
  3748. }
  3749. function Queue(capacity) {
  3750. this._capacity = capacity;
  3751. this._length = 0;
  3752. this._front = 0;
  3753. }
  3754. Queue.prototype._willBeOverCapacity = function (size) {
  3755. return this._capacity < size;
  3756. };
  3757. Queue.prototype._pushOne = function (arg) {
  3758. var length = this.length();
  3759. this._checkCapacity(length + 1);
  3760. var i = (this._front + length) & (this._capacity - 1);
  3761. this[i] = arg;
  3762. this._length = length + 1;
  3763. };
  3764. Queue.prototype.push = function (fn, receiver, arg) {
  3765. var length = this.length() + 3;
  3766. if (this._willBeOverCapacity(length)) {
  3767. this._pushOne(fn);
  3768. this._pushOne(receiver);
  3769. this._pushOne(arg);
  3770. return;
  3771. }
  3772. var j = this._front + length - 3;
  3773. this._checkCapacity(length);
  3774. var wrapMask = this._capacity - 1;
  3775. this[(j + 0) & wrapMask] = fn;
  3776. this[(j + 1) & wrapMask] = receiver;
  3777. this[(j + 2) & wrapMask] = arg;
  3778. this._length = length;
  3779. };
  3780. Queue.prototype.shift = function () {
  3781. var front = this._front,
  3782. ret = this[front];
  3783. this[front] = undefined;
  3784. this._front = (front + 1) & (this._capacity - 1);
  3785. this._length--;
  3786. return ret;
  3787. };
  3788. Queue.prototype.length = function () {
  3789. return this._length;
  3790. };
  3791. Queue.prototype._checkCapacity = function (size) {
  3792. if (this._capacity < size) {
  3793. this._resizeTo(this._capacity << 1);
  3794. }
  3795. };
  3796. Queue.prototype._resizeTo = function (capacity) {
  3797. var oldCapacity = this._capacity;
  3798. this._capacity = capacity;
  3799. var front = this._front;
  3800. var length = this._length;
  3801. var moveItemsCount = (front + length) & (oldCapacity - 1);
  3802. arrayMove(this, 0, this, oldCapacity, moveItemsCount);
  3803. };
  3804. module.exports = Queue;
  3805. },{}],27:[function(_dereq_,module,exports){
  3806. "use strict";
  3807. module.exports = function(
  3808. Promise, INTERNAL, tryConvertToPromise, apiRejection) {
  3809. var util = _dereq_("./util");
  3810. var raceLater = function (promise) {
  3811. return promise.then(function(array) {
  3812. return race(array, promise);
  3813. });
  3814. };
  3815. function race(promises, parent) {
  3816. var maybePromise = tryConvertToPromise(promises);
  3817. if (maybePromise instanceof Promise) {
  3818. return raceLater(maybePromise);
  3819. } else {
  3820. promises = util.asArray(promises);
  3821. if (promises === null)
  3822. return apiRejection("expecting an array or an iterable object but got " + util.classString(promises));
  3823. }
  3824. var ret = new Promise(INTERNAL);
  3825. if (parent !== undefined) {
  3826. ret._propagateFrom(parent, 3);
  3827. }
  3828. var fulfill = ret._fulfill;
  3829. var reject = ret._reject;
  3830. for (var i = 0, len = promises.length; i < len; ++i) {
  3831. var val = promises[i];
  3832. if (val === undefined && !(i in promises)) {
  3833. continue;
  3834. }
  3835. Promise.cast(val)._then(fulfill, reject, undefined, ret, null);
  3836. }
  3837. return ret;
  3838. }
  3839. Promise.race = function (promises) {
  3840. return race(promises, undefined);
  3841. };
  3842. Promise.prototype.race = function () {
  3843. return race(this, undefined);
  3844. };
  3845. };
  3846. },{"./util":36}],28:[function(_dereq_,module,exports){
  3847. "use strict";
  3848. module.exports = function(Promise,
  3849. PromiseArray,
  3850. apiRejection,
  3851. tryConvertToPromise,
  3852. INTERNAL,
  3853. debug) {
  3854. var getDomain = Promise._getDomain;
  3855. var util = _dereq_("./util");
  3856. var tryCatch = util.tryCatch;
  3857. function ReductionPromiseArray(promises, fn, initialValue, _each) {
  3858. this.constructor$(promises);
  3859. var domain = getDomain();
  3860. this._fn = domain === null ? fn : util.domainBind(domain, fn);
  3861. if (initialValue !== undefined) {
  3862. initialValue = Promise.resolve(initialValue);
  3863. initialValue._attachCancellationCallback(this);
  3864. }
  3865. this._initialValue = initialValue;
  3866. this._currentCancellable = null;
  3867. if(_each === INTERNAL) {
  3868. this._eachValues = Array(this._length);
  3869. } else if (_each === 0) {
  3870. this._eachValues = null;
  3871. } else {
  3872. this._eachValues = undefined;
  3873. }
  3874. this._promise._captureStackTrace();
  3875. this._init$(undefined, -5);
  3876. }
  3877. util.inherits(ReductionPromiseArray, PromiseArray);
  3878. ReductionPromiseArray.prototype._gotAccum = function(accum) {
  3879. if (this._eachValues !== undefined &&
  3880. this._eachValues !== null &&
  3881. accum !== INTERNAL) {
  3882. this._eachValues.push(accum);
  3883. }
  3884. };
  3885. ReductionPromiseArray.prototype._eachComplete = function(value) {
  3886. if (this._eachValues !== null) {
  3887. this._eachValues.push(value);
  3888. }
  3889. return this._eachValues;
  3890. };
  3891. ReductionPromiseArray.prototype._init = function() {};
  3892. ReductionPromiseArray.prototype._resolveEmptyArray = function() {
  3893. this._resolve(this._eachValues !== undefined ? this._eachValues
  3894. : this._initialValue);
  3895. };
  3896. ReductionPromiseArray.prototype.shouldCopyValues = function () {
  3897. return false;
  3898. };
  3899. ReductionPromiseArray.prototype._resolve = function(value) {
  3900. this._promise._resolveCallback(value);
  3901. this._values = null;
  3902. };
  3903. ReductionPromiseArray.prototype._resultCancelled = function(sender) {
  3904. if (sender === this._initialValue) return this._cancel();
  3905. if (this._isResolved()) return;
  3906. this._resultCancelled$();
  3907. if (this._currentCancellable instanceof Promise) {
  3908. this._currentCancellable.cancel();
  3909. }
  3910. if (this._initialValue instanceof Promise) {
  3911. this._initialValue.cancel();
  3912. }
  3913. };
  3914. ReductionPromiseArray.prototype._iterate = function (values) {
  3915. this._values = values;
  3916. var value;
  3917. var i;
  3918. var length = values.length;
  3919. if (this._initialValue !== undefined) {
  3920. value = this._initialValue;
  3921. i = 0;
  3922. } else {
  3923. value = Promise.resolve(values[0]);
  3924. i = 1;
  3925. }
  3926. this._currentCancellable = value;
  3927. if (!value.isRejected()) {
  3928. for (; i < length; ++i) {
  3929. var ctx = {
  3930. accum: null,
  3931. value: values[i],
  3932. index: i,
  3933. length: length,
  3934. array: this
  3935. };
  3936. value = value._then(gotAccum, undefined, undefined, ctx, undefined);
  3937. }
  3938. }
  3939. if (this._eachValues !== undefined) {
  3940. value = value
  3941. ._then(this._eachComplete, undefined, undefined, this, undefined);
  3942. }
  3943. value._then(completed, completed, undefined, value, this);
  3944. };
  3945. Promise.prototype.reduce = function (fn, initialValue) {
  3946. return reduce(this, fn, initialValue, null);
  3947. };
  3948. Promise.reduce = function (promises, fn, initialValue, _each) {
  3949. return reduce(promises, fn, initialValue, _each);
  3950. };
  3951. function completed(valueOrReason, array) {
  3952. if (this.isFulfilled()) {
  3953. array._resolve(valueOrReason);
  3954. } else {
  3955. array._reject(valueOrReason);
  3956. }
  3957. }
  3958. function reduce(promises, fn, initialValue, _each) {
  3959. if (typeof fn !== "function") {
  3960. return apiRejection("expecting a function but got " + util.classString(fn));
  3961. }
  3962. var array = new ReductionPromiseArray(promises, fn, initialValue, _each);
  3963. return array.promise();
  3964. }
  3965. function gotAccum(accum) {
  3966. this.accum = accum;
  3967. this.array._gotAccum(accum);
  3968. var value = tryConvertToPromise(this.value, this.array._promise);
  3969. if (value instanceof Promise) {
  3970. this.array._currentCancellable = value;
  3971. return value._then(gotValue, undefined, undefined, this, undefined);
  3972. } else {
  3973. return gotValue.call(this, value);
  3974. }
  3975. }
  3976. function gotValue(value) {
  3977. var array = this.array;
  3978. var promise = array._promise;
  3979. var fn = tryCatch(array._fn);
  3980. promise._pushContext();
  3981. var ret;
  3982. if (array._eachValues !== undefined) {
  3983. ret = fn.call(promise._boundValue(), value, this.index, this.length);
  3984. } else {
  3985. ret = fn.call(promise._boundValue(),
  3986. this.accum, value, this.index, this.length);
  3987. }
  3988. if (ret instanceof Promise) {
  3989. array._currentCancellable = ret;
  3990. }
  3991. var promiseCreated = promise._popContext();
  3992. debug.checkForgottenReturns(
  3993. ret,
  3994. promiseCreated,
  3995. array._eachValues !== undefined ? "Promise.each" : "Promise.reduce",
  3996. promise
  3997. );
  3998. return ret;
  3999. }
  4000. };
  4001. },{"./util":36}],29:[function(_dereq_,module,exports){
  4002. "use strict";
  4003. var util = _dereq_("./util");
  4004. var schedule;
  4005. var noAsyncScheduler = function() {
  4006. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4007. };
  4008. var NativePromise = util.getNativePromise();
  4009. if (util.isNode && typeof MutationObserver === "undefined") {
  4010. var GlobalSetImmediate = global.setImmediate;
  4011. var ProcessNextTick = process.nextTick;
  4012. schedule = util.isRecentNode
  4013. ? function(fn) { GlobalSetImmediate.call(global, fn); }
  4014. : function(fn) { ProcessNextTick.call(process, fn); };
  4015. } else if (typeof NativePromise === "function" &&
  4016. typeof NativePromise.resolve === "function") {
  4017. var nativePromise = NativePromise.resolve();
  4018. schedule = function(fn) {
  4019. nativePromise.then(fn);
  4020. };
  4021. } else if ((typeof MutationObserver !== "undefined") &&
  4022. !(typeof window !== "undefined" &&
  4023. window.navigator &&
  4024. (window.navigator.standalone || window.cordova))) {
  4025. schedule = (function() {
  4026. var div = document.createElement("div");
  4027. var opts = {attributes: true};
  4028. var toggleScheduled = false;
  4029. var div2 = document.createElement("div");
  4030. var o2 = new MutationObserver(function() {
  4031. div.classList.toggle("foo");
  4032. toggleScheduled = false;
  4033. });
  4034. o2.observe(div2, opts);
  4035. var scheduleToggle = function() {
  4036. if (toggleScheduled) return;
  4037. toggleScheduled = true;
  4038. div2.classList.toggle("foo");
  4039. };
  4040. return function schedule(fn) {
  4041. var o = new MutationObserver(function() {
  4042. o.disconnect();
  4043. fn();
  4044. });
  4045. o.observe(div, opts);
  4046. scheduleToggle();
  4047. };
  4048. })();
  4049. } else if (typeof setImmediate !== "undefined") {
  4050. schedule = function (fn) {
  4051. setImmediate(fn);
  4052. };
  4053. } else if (typeof setTimeout !== "undefined") {
  4054. schedule = function (fn) {
  4055. setTimeout(fn, 0);
  4056. };
  4057. } else {
  4058. schedule = noAsyncScheduler;
  4059. }
  4060. module.exports = schedule;
  4061. },{"./util":36}],30:[function(_dereq_,module,exports){
  4062. "use strict";
  4063. module.exports =
  4064. function(Promise, PromiseArray, debug) {
  4065. var PromiseInspection = Promise.PromiseInspection;
  4066. var util = _dereq_("./util");
  4067. function SettledPromiseArray(values) {
  4068. this.constructor$(values);
  4069. }
  4070. util.inherits(SettledPromiseArray, PromiseArray);
  4071. SettledPromiseArray.prototype._promiseResolved = function (index, inspection) {
  4072. this._values[index] = inspection;
  4073. var totalResolved = ++this._totalResolved;
  4074. if (totalResolved >= this._length) {
  4075. this._resolve(this._values);
  4076. return true;
  4077. }
  4078. return false;
  4079. };
  4080. SettledPromiseArray.prototype._promiseFulfilled = function (value, index) {
  4081. var ret = new PromiseInspection();
  4082. ret._bitField = 33554432;
  4083. ret._settledValueField = value;
  4084. return this._promiseResolved(index, ret);
  4085. };
  4086. SettledPromiseArray.prototype._promiseRejected = function (reason, index) {
  4087. var ret = new PromiseInspection();
  4088. ret._bitField = 16777216;
  4089. ret._settledValueField = reason;
  4090. return this._promiseResolved(index, ret);
  4091. };
  4092. Promise.settle = function (promises) {
  4093. debug.deprecated(".settle()", ".reflect()");
  4094. return new SettledPromiseArray(promises).promise();
  4095. };
  4096. Promise.prototype.settle = function () {
  4097. return Promise.settle(this);
  4098. };
  4099. };
  4100. },{"./util":36}],31:[function(_dereq_,module,exports){
  4101. "use strict";
  4102. module.exports =
  4103. function(Promise, PromiseArray, apiRejection) {
  4104. var util = _dereq_("./util");
  4105. var RangeError = _dereq_("./errors").RangeError;
  4106. var AggregateError = _dereq_("./errors").AggregateError;
  4107. var isArray = util.isArray;
  4108. var CANCELLATION = {};
  4109. function SomePromiseArray(values) {
  4110. this.constructor$(values);
  4111. this._howMany = 0;
  4112. this._unwrap = false;
  4113. this._initialized = false;
  4114. }
  4115. util.inherits(SomePromiseArray, PromiseArray);
  4116. SomePromiseArray.prototype._init = function () {
  4117. if (!this._initialized) {
  4118. return;
  4119. }
  4120. if (this._howMany === 0) {
  4121. this._resolve([]);
  4122. return;
  4123. }
  4124. this._init$(undefined, -5);
  4125. var isArrayResolved = isArray(this._values);
  4126. if (!this._isResolved() &&
  4127. isArrayResolved &&
  4128. this._howMany > this._canPossiblyFulfill()) {
  4129. this._reject(this._getRangeError(this.length()));
  4130. }
  4131. };
  4132. SomePromiseArray.prototype.init = function () {
  4133. this._initialized = true;
  4134. this._init();
  4135. };
  4136. SomePromiseArray.prototype.setUnwrap = function () {
  4137. this._unwrap = true;
  4138. };
  4139. SomePromiseArray.prototype.howMany = function () {
  4140. return this._howMany;
  4141. };
  4142. SomePromiseArray.prototype.setHowMany = function (count) {
  4143. this._howMany = count;
  4144. };
  4145. SomePromiseArray.prototype._promiseFulfilled = function (value) {
  4146. this._addFulfilled(value);
  4147. if (this._fulfilled() === this.howMany()) {
  4148. this._values.length = this.howMany();
  4149. if (this.howMany() === 1 && this._unwrap) {
  4150. this._resolve(this._values[0]);
  4151. } else {
  4152. this._resolve(this._values);
  4153. }
  4154. return true;
  4155. }
  4156. return false;
  4157. };
  4158. SomePromiseArray.prototype._promiseRejected = function (reason) {
  4159. this._addRejected(reason);
  4160. return this._checkOutcome();
  4161. };
  4162. SomePromiseArray.prototype._promiseCancelled = function () {
  4163. if (this._values instanceof Promise || this._values == null) {
  4164. return this._cancel();
  4165. }
  4166. this._addRejected(CANCELLATION);
  4167. return this._checkOutcome();
  4168. };
  4169. SomePromiseArray.prototype._checkOutcome = function() {
  4170. if (this.howMany() > this._canPossiblyFulfill()) {
  4171. var e = new AggregateError();
  4172. for (var i = this.length(); i < this._values.length; ++i) {
  4173. if (this._values[i] !== CANCELLATION) {
  4174. e.push(this._values[i]);
  4175. }
  4176. }
  4177. if (e.length > 0) {
  4178. this._reject(e);
  4179. } else {
  4180. this._cancel();
  4181. }
  4182. return true;
  4183. }
  4184. return false;
  4185. };
  4186. SomePromiseArray.prototype._fulfilled = function () {
  4187. return this._totalResolved;
  4188. };
  4189. SomePromiseArray.prototype._rejected = function () {
  4190. return this._values.length - this.length();
  4191. };
  4192. SomePromiseArray.prototype._addRejected = function (reason) {
  4193. this._values.push(reason);
  4194. };
  4195. SomePromiseArray.prototype._addFulfilled = function (value) {
  4196. this._values[this._totalResolved++] = value;
  4197. };
  4198. SomePromiseArray.prototype._canPossiblyFulfill = function () {
  4199. return this.length() - this._rejected();
  4200. };
  4201. SomePromiseArray.prototype._getRangeError = function (count) {
  4202. var message = "Input array must contain at least " +
  4203. this._howMany + " items but contains only " + count + " items";
  4204. return new RangeError(message);
  4205. };
  4206. SomePromiseArray.prototype._resolveEmptyArray = function () {
  4207. this._reject(this._getRangeError(0));
  4208. };
  4209. function some(promises, howMany) {
  4210. if ((howMany | 0) !== howMany || howMany < 0) {
  4211. return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4212. }
  4213. var ret = new SomePromiseArray(promises);
  4214. var promise = ret.promise();
  4215. ret.setHowMany(howMany);
  4216. ret.init();
  4217. return promise;
  4218. }
  4219. Promise.some = function (promises, howMany) {
  4220. return some(promises, howMany);
  4221. };
  4222. Promise.prototype.some = function (howMany) {
  4223. return some(this, howMany);
  4224. };
  4225. Promise._SomePromiseArray = SomePromiseArray;
  4226. };
  4227. },{"./errors":12,"./util":36}],32:[function(_dereq_,module,exports){
  4228. "use strict";
  4229. module.exports = function(Promise) {
  4230. function PromiseInspection(promise) {
  4231. if (promise !== undefined) {
  4232. promise = promise._target();
  4233. this._bitField = promise._bitField;
  4234. this._settledValueField = promise._isFateSealed()
  4235. ? promise._settledValue() : undefined;
  4236. }
  4237. else {
  4238. this._bitField = 0;
  4239. this._settledValueField = undefined;
  4240. }
  4241. }
  4242. PromiseInspection.prototype._settledValue = function() {
  4243. return this._settledValueField;
  4244. };
  4245. var value = PromiseInspection.prototype.value = function () {
  4246. if (!this.isFulfilled()) {
  4247. throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4248. }
  4249. return this._settledValue();
  4250. };
  4251. var reason = PromiseInspection.prototype.error =
  4252. PromiseInspection.prototype.reason = function () {
  4253. if (!this.isRejected()) {
  4254. throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4255. }
  4256. return this._settledValue();
  4257. };
  4258. var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
  4259. return (this._bitField & 33554432) !== 0;
  4260. };
  4261. var isRejected = PromiseInspection.prototype.isRejected = function () {
  4262. return (this._bitField & 16777216) !== 0;
  4263. };
  4264. var isPending = PromiseInspection.prototype.isPending = function () {
  4265. return (this._bitField & 50397184) === 0;
  4266. };
  4267. var isResolved = PromiseInspection.prototype.isResolved = function () {
  4268. return (this._bitField & 50331648) !== 0;
  4269. };
  4270. PromiseInspection.prototype.isCancelled = function() {
  4271. return (this._bitField & 8454144) !== 0;
  4272. };
  4273. Promise.prototype.__isCancelled = function() {
  4274. return (this._bitField & 65536) === 65536;
  4275. };
  4276. Promise.prototype._isCancelled = function() {
  4277. return this._target().__isCancelled();
  4278. };
  4279. Promise.prototype.isCancelled = function() {
  4280. return (this._target()._bitField & 8454144) !== 0;
  4281. };
  4282. Promise.prototype.isPending = function() {
  4283. return isPending.call(this._target());
  4284. };
  4285. Promise.prototype.isRejected = function() {
  4286. return isRejected.call(this._target());
  4287. };
  4288. Promise.prototype.isFulfilled = function() {
  4289. return isFulfilled.call(this._target());
  4290. };
  4291. Promise.prototype.isResolved = function() {
  4292. return isResolved.call(this._target());
  4293. };
  4294. Promise.prototype.value = function() {
  4295. return value.call(this._target());
  4296. };
  4297. Promise.prototype.reason = function() {
  4298. var target = this._target();
  4299. target._unsetRejectionIsUnhandled();
  4300. return reason.call(target);
  4301. };
  4302. Promise.prototype._value = function() {
  4303. return this._settledValue();
  4304. };
  4305. Promise.prototype._reason = function() {
  4306. this._unsetRejectionIsUnhandled();
  4307. return this._settledValue();
  4308. };
  4309. Promise.PromiseInspection = PromiseInspection;
  4310. };
  4311. },{}],33:[function(_dereq_,module,exports){
  4312. "use strict";
  4313. module.exports = function(Promise, INTERNAL) {
  4314. var util = _dereq_("./util");
  4315. var errorObj = util.errorObj;
  4316. var isObject = util.isObject;
  4317. function tryConvertToPromise(obj, context) {
  4318. if (isObject(obj)) {
  4319. if (obj instanceof Promise) return obj;
  4320. var then = getThen(obj);
  4321. if (then === errorObj) {
  4322. if (context) context._pushContext();
  4323. var ret = Promise.reject(then.e);
  4324. if (context) context._popContext();
  4325. return ret;
  4326. } else if (typeof then === "function") {
  4327. if (isAnyBluebirdPromise(obj)) {
  4328. var ret = new Promise(INTERNAL);
  4329. obj._then(
  4330. ret._fulfill,
  4331. ret._reject,
  4332. undefined,
  4333. ret,
  4334. null
  4335. );
  4336. return ret;
  4337. }
  4338. return doThenable(obj, then, context);
  4339. }
  4340. }
  4341. return obj;
  4342. }
  4343. function doGetThen(obj) {
  4344. return obj.then;
  4345. }
  4346. function getThen(obj) {
  4347. try {
  4348. return doGetThen(obj);
  4349. } catch (e) {
  4350. errorObj.e = e;
  4351. return errorObj;
  4352. }
  4353. }
  4354. var hasProp = {}.hasOwnProperty;
  4355. function isAnyBluebirdPromise(obj) {
  4356. try {
  4357. return hasProp.call(obj, "_promise0");
  4358. } catch (e) {
  4359. return false;
  4360. }
  4361. }
  4362. function doThenable(x, then, context) {
  4363. var promise = new Promise(INTERNAL);
  4364. var ret = promise;
  4365. if (context) context._pushContext();
  4366. promise._captureStackTrace();
  4367. if (context) context._popContext();
  4368. var synchronous = true;
  4369. var result = util.tryCatch(then).call(x, resolve, reject);
  4370. synchronous = false;
  4371. if (promise && result === errorObj) {
  4372. promise._rejectCallback(result.e, true, true);
  4373. promise = null;
  4374. }
  4375. function resolve(value) {
  4376. if (!promise) return;
  4377. promise._resolveCallback(value);
  4378. promise = null;
  4379. }
  4380. function reject(reason) {
  4381. if (!promise) return;
  4382. promise._rejectCallback(reason, synchronous, true);
  4383. promise = null;
  4384. }
  4385. return ret;
  4386. }
  4387. return tryConvertToPromise;
  4388. };
  4389. },{"./util":36}],34:[function(_dereq_,module,exports){
  4390. "use strict";
  4391. module.exports = function(Promise, INTERNAL, debug) {
  4392. var util = _dereq_("./util");
  4393. var TimeoutError = Promise.TimeoutError;
  4394. function HandleWrapper(handle) {
  4395. this.handle = handle;
  4396. }
  4397. HandleWrapper.prototype._resultCancelled = function() {
  4398. clearTimeout(this.handle);
  4399. };
  4400. var afterValue = function(value) { return delay(+this).thenReturn(value); };
  4401. var delay = Promise.delay = function (ms, value) {
  4402. var ret;
  4403. var handle;
  4404. if (value !== undefined) {
  4405. ret = Promise.resolve(value)
  4406. ._then(afterValue, null, null, ms, undefined);
  4407. if (debug.cancellation() && value instanceof Promise) {
  4408. ret._setOnCancel(value);
  4409. }
  4410. } else {
  4411. ret = new Promise(INTERNAL);
  4412. handle = setTimeout(function() { ret._fulfill(); }, +ms);
  4413. if (debug.cancellation()) {
  4414. ret._setOnCancel(new HandleWrapper(handle));
  4415. }
  4416. ret._captureStackTrace();
  4417. }
  4418. ret._setAsyncGuaranteed();
  4419. return ret;
  4420. };
  4421. Promise.prototype.delay = function (ms) {
  4422. return delay(ms, this);
  4423. };
  4424. var afterTimeout = function (promise, message, parent) {
  4425. var err;
  4426. if (typeof message !== "string") {
  4427. if (message instanceof Error) {
  4428. err = message;
  4429. } else {
  4430. err = new TimeoutError("operation timed out");
  4431. }
  4432. } else {
  4433. err = new TimeoutError(message);
  4434. }
  4435. util.markAsOriginatingFromRejection(err);
  4436. promise._attachExtraTrace(err);
  4437. promise._reject(err);
  4438. if (parent != null) {
  4439. parent.cancel();
  4440. }
  4441. };
  4442. function successClear(value) {
  4443. clearTimeout(this.handle);
  4444. return value;
  4445. }
  4446. function failureClear(reason) {
  4447. clearTimeout(this.handle);
  4448. throw reason;
  4449. }
  4450. Promise.prototype.timeout = function (ms, message) {
  4451. ms = +ms;
  4452. var ret, parent;
  4453. var handleWrapper = new HandleWrapper(setTimeout(function timeoutTimeout() {
  4454. if (ret.isPending()) {
  4455. afterTimeout(ret, message, parent);
  4456. }
  4457. }, ms));
  4458. if (debug.cancellation()) {
  4459. parent = this.then();
  4460. ret = parent._then(successClear, failureClear,
  4461. undefined, handleWrapper, undefined);
  4462. ret._setOnCancel(handleWrapper);
  4463. } else {
  4464. ret = this._then(successClear, failureClear,
  4465. undefined, handleWrapper, undefined);
  4466. }
  4467. return ret;
  4468. };
  4469. };
  4470. },{"./util":36}],35:[function(_dereq_,module,exports){
  4471. "use strict";
  4472. module.exports = function (Promise, apiRejection, tryConvertToPromise,
  4473. createContext, INTERNAL, debug) {
  4474. var util = _dereq_("./util");
  4475. var TypeError = _dereq_("./errors").TypeError;
  4476. var inherits = _dereq_("./util").inherits;
  4477. var errorObj = util.errorObj;
  4478. var tryCatch = util.tryCatch;
  4479. var NULL = {};
  4480. function thrower(e) {
  4481. setTimeout(function(){throw e;}, 0);
  4482. }
  4483. function castPreservingDisposable(thenable) {
  4484. var maybePromise = tryConvertToPromise(thenable);
  4485. if (maybePromise !== thenable &&
  4486. typeof thenable._isDisposable === "function" &&
  4487. typeof thenable._getDisposer === "function" &&
  4488. thenable._isDisposable()) {
  4489. maybePromise._setDisposable(thenable._getDisposer());
  4490. }
  4491. return maybePromise;
  4492. }
  4493. function dispose(resources, inspection) {
  4494. var i = 0;
  4495. var len = resources.length;
  4496. var ret = new Promise(INTERNAL);
  4497. function iterator() {
  4498. if (i >= len) return ret._fulfill();
  4499. var maybePromise = castPreservingDisposable(resources[i++]);
  4500. if (maybePromise instanceof Promise &&
  4501. maybePromise._isDisposable()) {
  4502. try {
  4503. maybePromise = tryConvertToPromise(
  4504. maybePromise._getDisposer().tryDispose(inspection),
  4505. resources.promise);
  4506. } catch (e) {
  4507. return thrower(e);
  4508. }
  4509. if (maybePromise instanceof Promise) {
  4510. return maybePromise._then(iterator, thrower,
  4511. null, null, null);
  4512. }
  4513. }
  4514. iterator();
  4515. }
  4516. iterator();
  4517. return ret;
  4518. }
  4519. function Disposer(data, promise, context) {
  4520. this._data = data;
  4521. this._promise = promise;
  4522. this._context = context;
  4523. }
  4524. Disposer.prototype.data = function () {
  4525. return this._data;
  4526. };
  4527. Disposer.prototype.promise = function () {
  4528. return this._promise;
  4529. };
  4530. Disposer.prototype.resource = function () {
  4531. if (this.promise().isFulfilled()) {
  4532. return this.promise().value();
  4533. }
  4534. return NULL;
  4535. };
  4536. Disposer.prototype.tryDispose = function(inspection) {
  4537. var resource = this.resource();
  4538. var context = this._context;
  4539. if (context !== undefined) context._pushContext();
  4540. var ret = resource !== NULL
  4541. ? this.doDispose(resource, inspection) : null;
  4542. if (context !== undefined) context._popContext();
  4543. this._promise._unsetDisposable();
  4544. this._data = null;
  4545. return ret;
  4546. };
  4547. Disposer.isDisposer = function (d) {
  4548. return (d != null &&
  4549. typeof d.resource === "function" &&
  4550. typeof d.tryDispose === "function");
  4551. };
  4552. function FunctionDisposer(fn, promise, context) {
  4553. this.constructor$(fn, promise, context);
  4554. }
  4555. inherits(FunctionDisposer, Disposer);
  4556. FunctionDisposer.prototype.doDispose = function (resource, inspection) {
  4557. var fn = this.data();
  4558. return fn.call(resource, resource, inspection);
  4559. };
  4560. function maybeUnwrapDisposer(value) {
  4561. if (Disposer.isDisposer(value)) {
  4562. this.resources[this.index]._setDisposable(value);
  4563. return value.promise();
  4564. }
  4565. return value;
  4566. }
  4567. function ResourceList(length) {
  4568. this.length = length;
  4569. this.promise = null;
  4570. this[length-1] = null;
  4571. }
  4572. ResourceList.prototype._resultCancelled = function() {
  4573. var len = this.length;
  4574. for (var i = 0; i < len; ++i) {
  4575. var item = this[i];
  4576. if (item instanceof Promise) {
  4577. item.cancel();
  4578. }
  4579. }
  4580. };
  4581. Promise.using = function () {
  4582. var len = arguments.length;
  4583. if (len < 2) return apiRejection(
  4584. "you must pass at least 2 arguments to Promise.using");
  4585. var fn = arguments[len - 1];
  4586. if (typeof fn !== "function") {
  4587. return apiRejection("expecting a function but got " + util.classString(fn));
  4588. }
  4589. var input;
  4590. var spreadArgs = true;
  4591. if (len === 2 && Array.isArray(arguments[0])) {
  4592. input = arguments[0];
  4593. len = input.length;
  4594. spreadArgs = false;
  4595. } else {
  4596. input = arguments;
  4597. len--;
  4598. }
  4599. var resources = new ResourceList(len);
  4600. for (var i = 0; i < len; ++i) {
  4601. var resource = input[i];
  4602. if (Disposer.isDisposer(resource)) {
  4603. var disposer = resource;
  4604. resource = resource.promise();
  4605. resource._setDisposable(disposer);
  4606. } else {
  4607. var maybePromise = tryConvertToPromise(resource);
  4608. if (maybePromise instanceof Promise) {
  4609. resource =
  4610. maybePromise._then(maybeUnwrapDisposer, null, null, {
  4611. resources: resources,
  4612. index: i
  4613. }, undefined);
  4614. }
  4615. }
  4616. resources[i] = resource;
  4617. }
  4618. var reflectedResources = new Array(resources.length);
  4619. for (var i = 0; i < reflectedResources.length; ++i) {
  4620. reflectedResources[i] = Promise.resolve(resources[i]).reflect();
  4621. }
  4622. var resultPromise = Promise.all(reflectedResources)
  4623. .then(function(inspections) {
  4624. for (var i = 0; i < inspections.length; ++i) {
  4625. var inspection = inspections[i];
  4626. if (inspection.isRejected()) {
  4627. errorObj.e = inspection.error();
  4628. return errorObj;
  4629. } else if (!inspection.isFulfilled()) {
  4630. resultPromise.cancel();
  4631. return;
  4632. }
  4633. inspections[i] = inspection.value();
  4634. }
  4635. promise._pushContext();
  4636. fn = tryCatch(fn);
  4637. var ret = spreadArgs
  4638. ? fn.apply(undefined, inspections) : fn(inspections);
  4639. var promiseCreated = promise._popContext();
  4640. debug.checkForgottenReturns(
  4641. ret, promiseCreated, "Promise.using", promise);
  4642. return ret;
  4643. });
  4644. var promise = resultPromise.lastly(function() {
  4645. var inspection = new Promise.PromiseInspection(resultPromise);
  4646. return dispose(resources, inspection);
  4647. });
  4648. resources.promise = promise;
  4649. promise._setOnCancel(resources);
  4650. return promise;
  4651. };
  4652. Promise.prototype._setDisposable = function (disposer) {
  4653. this._bitField = this._bitField | 131072;
  4654. this._disposer = disposer;
  4655. };
  4656. Promise.prototype._isDisposable = function () {
  4657. return (this._bitField & 131072) > 0;
  4658. };
  4659. Promise.prototype._getDisposer = function () {
  4660. return this._disposer;
  4661. };
  4662. Promise.prototype._unsetDisposable = function () {
  4663. this._bitField = this._bitField & (~131072);
  4664. this._disposer = undefined;
  4665. };
  4666. Promise.prototype.disposer = function (fn) {
  4667. if (typeof fn === "function") {
  4668. return new FunctionDisposer(fn, this, createContext());
  4669. }
  4670. throw new TypeError();
  4671. };
  4672. };
  4673. },{"./errors":12,"./util":36}],36:[function(_dereq_,module,exports){
  4674. "use strict";
  4675. var es5 = _dereq_("./es5");
  4676. var canEvaluate = typeof navigator == "undefined";
  4677. var errorObj = {e: {}};
  4678. var tryCatchTarget;
  4679. var globalObject = typeof self !== "undefined" ? self :
  4680. typeof window !== "undefined" ? window :
  4681. typeof global !== "undefined" ? global :
  4682. this !== undefined ? this : null;
  4683. function tryCatcher() {
  4684. try {
  4685. var target = tryCatchTarget;
  4686. tryCatchTarget = null;
  4687. return target.apply(this, arguments);
  4688. } catch (e) {
  4689. errorObj.e = e;
  4690. return errorObj;
  4691. }
  4692. }
  4693. function tryCatch(fn) {
  4694. tryCatchTarget = fn;
  4695. return tryCatcher;
  4696. }
  4697. var inherits = function(Child, Parent) {
  4698. var hasProp = {}.hasOwnProperty;
  4699. function T() {
  4700. this.constructor = Child;
  4701. this.constructor$ = Parent;
  4702. for (var propertyName in Parent.prototype) {
  4703. if (hasProp.call(Parent.prototype, propertyName) &&
  4704. propertyName.charAt(propertyName.length-1) !== "$"
  4705. ) {
  4706. this[propertyName + "$"] = Parent.prototype[propertyName];
  4707. }
  4708. }
  4709. }
  4710. T.prototype = Parent.prototype;
  4711. Child.prototype = new T();
  4712. return Child.prototype;
  4713. };
  4714. function isPrimitive(val) {
  4715. return val == null || val === true || val === false ||
  4716. typeof val === "string" || typeof val === "number";
  4717. }
  4718. function isObject(value) {
  4719. return typeof value === "function" ||
  4720. typeof value === "object" && value !== null;
  4721. }
  4722. function maybeWrapAsError(maybeError) {
  4723. if (!isPrimitive(maybeError)) return maybeError;
  4724. return new Error(safeToString(maybeError));
  4725. }
  4726. function withAppended(target, appendee) {
  4727. var len = target.length;
  4728. var ret = new Array(len + 1);
  4729. var i;
  4730. for (i = 0; i < len; ++i) {
  4731. ret[i] = target[i];
  4732. }
  4733. ret[i] = appendee;
  4734. return ret;
  4735. }
  4736. function getDataPropertyOrDefault(obj, key, defaultValue) {
  4737. if (es5.isES5) {
  4738. var desc = Object.getOwnPropertyDescriptor(obj, key);
  4739. if (desc != null) {
  4740. return desc.get == null && desc.set == null
  4741. ? desc.value
  4742. : defaultValue;
  4743. }
  4744. } else {
  4745. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  4746. }
  4747. }
  4748. function notEnumerableProp(obj, name, value) {
  4749. if (isPrimitive(obj)) return obj;
  4750. var descriptor = {
  4751. value: value,
  4752. configurable: true,
  4753. enumerable: false,
  4754. writable: true
  4755. };
  4756. es5.defineProperty(obj, name, descriptor);
  4757. return obj;
  4758. }
  4759. function thrower(r) {
  4760. throw r;
  4761. }
  4762. var inheritedDataKeys = (function() {
  4763. var excludedPrototypes = [
  4764. Array.prototype,
  4765. Object.prototype,
  4766. Function.prototype
  4767. ];
  4768. var isExcludedProto = function(val) {
  4769. for (var i = 0; i < excludedPrototypes.length; ++i) {
  4770. if (excludedPrototypes[i] === val) {
  4771. return true;
  4772. }
  4773. }
  4774. return false;
  4775. };
  4776. if (es5.isES5) {
  4777. var getKeys = Object.getOwnPropertyNames;
  4778. return function(obj) {
  4779. var ret = [];
  4780. var visitedKeys = Object.create(null);
  4781. while (obj != null && !isExcludedProto(obj)) {
  4782. var keys;
  4783. try {
  4784. keys = getKeys(obj);
  4785. } catch (e) {
  4786. return ret;
  4787. }
  4788. for (var i = 0; i < keys.length; ++i) {
  4789. var key = keys[i];
  4790. if (visitedKeys[key]) continue;
  4791. visitedKeys[key] = true;
  4792. var desc = Object.getOwnPropertyDescriptor(obj, key);
  4793. if (desc != null && desc.get == null && desc.set == null) {
  4794. ret.push(key);
  4795. }
  4796. }
  4797. obj = es5.getPrototypeOf(obj);
  4798. }
  4799. return ret;
  4800. };
  4801. } else {
  4802. var hasProp = {}.hasOwnProperty;
  4803. return function(obj) {
  4804. if (isExcludedProto(obj)) return [];
  4805. var ret = [];
  4806. /*jshint forin:false */
  4807. enumeration: for (var key in obj) {
  4808. if (hasProp.call(obj, key)) {
  4809. ret.push(key);
  4810. } else {
  4811. for (var i = 0; i < excludedPrototypes.length; ++i) {
  4812. if (hasProp.call(excludedPrototypes[i], key)) {
  4813. continue enumeration;
  4814. }
  4815. }
  4816. ret.push(key);
  4817. }
  4818. }
  4819. return ret;
  4820. };
  4821. }
  4822. })();
  4823. var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
  4824. function isClass(fn) {
  4825. try {
  4826. if (typeof fn === "function") {
  4827. var keys = es5.names(fn.prototype);
  4828. var hasMethods = es5.isES5 && keys.length > 1;
  4829. var hasMethodsOtherThanConstructor = keys.length > 0 &&
  4830. !(keys.length === 1 && keys[0] === "constructor");
  4831. var hasThisAssignmentAndStaticMethods =
  4832. thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
  4833. if (hasMethods || hasMethodsOtherThanConstructor ||
  4834. hasThisAssignmentAndStaticMethods) {
  4835. return true;
  4836. }
  4837. }
  4838. return false;
  4839. } catch (e) {
  4840. return false;
  4841. }
  4842. }
  4843. function toFastProperties(obj) {
  4844. /*jshint -W027,-W055,-W031*/
  4845. function FakeConstructor() {}
  4846. FakeConstructor.prototype = obj;
  4847. var l = 8;
  4848. while (l--) new FakeConstructor();
  4849. return obj;
  4850. eval(obj);
  4851. }
  4852. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  4853. function isIdentifier(str) {
  4854. return rident.test(str);
  4855. }
  4856. function filledRange(count, prefix, suffix) {
  4857. var ret = new Array(count);
  4858. for(var i = 0; i < count; ++i) {
  4859. ret[i] = prefix + i + suffix;
  4860. }
  4861. return ret;
  4862. }
  4863. function safeToString(obj) {
  4864. try {
  4865. return obj + "";
  4866. } catch (e) {
  4867. return "[no string representation]";
  4868. }
  4869. }
  4870. function isError(obj) {
  4871. return obj instanceof Error ||
  4872. (obj !== null &&
  4873. typeof obj === "object" &&
  4874. typeof obj.message === "string" &&
  4875. typeof obj.name === "string");
  4876. }
  4877. function markAsOriginatingFromRejection(e) {
  4878. try {
  4879. notEnumerableProp(e, "isOperational", true);
  4880. }
  4881. catch(ignore) {}
  4882. }
  4883. function originatesFromRejection(e) {
  4884. if (e == null) return false;
  4885. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  4886. e["isOperational"] === true);
  4887. }
  4888. function canAttachTrace(obj) {
  4889. return isError(obj) && es5.propertyIsWritable(obj, "stack");
  4890. }
  4891. var ensureErrorObject = (function() {
  4892. if (!("stack" in new Error())) {
  4893. return function(value) {
  4894. if (canAttachTrace(value)) return value;
  4895. try {throw new Error(safeToString(value));}
  4896. catch(err) {return err;}
  4897. };
  4898. } else {
  4899. return function(value) {
  4900. if (canAttachTrace(value)) return value;
  4901. return new Error(safeToString(value));
  4902. };
  4903. }
  4904. })();
  4905. function classString(obj) {
  4906. return {}.toString.call(obj);
  4907. }
  4908. function copyDescriptors(from, to, filter) {
  4909. var keys = es5.names(from);
  4910. for (var i = 0; i < keys.length; ++i) {
  4911. var key = keys[i];
  4912. if (filter(key)) {
  4913. try {
  4914. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  4915. } catch (ignore) {}
  4916. }
  4917. }
  4918. }
  4919. var asArray = function(v) {
  4920. if (es5.isArray(v)) {
  4921. return v;
  4922. }
  4923. return null;
  4924. };
  4925. if (typeof Symbol !== "undefined" && Symbol.iterator) {
  4926. var ArrayFrom = typeof Array.from === "function" ? function(v) {
  4927. return Array.from(v);
  4928. } : function(v) {
  4929. var ret = [];
  4930. var it = v[Symbol.iterator]();
  4931. var itResult;
  4932. while (!((itResult = it.next()).done)) {
  4933. ret.push(itResult.value);
  4934. }
  4935. return ret;
  4936. };
  4937. asArray = function(v) {
  4938. if (es5.isArray(v)) {
  4939. return v;
  4940. } else if (v != null && typeof v[Symbol.iterator] === "function") {
  4941. return ArrayFrom(v);
  4942. }
  4943. return null;
  4944. };
  4945. }
  4946. var isNode = typeof process !== "undefined" &&
  4947. classString(process).toLowerCase() === "[object process]";
  4948. var hasEnvVariables = typeof process !== "undefined" &&
  4949. typeof process.env !== "undefined";
  4950. function env(key) {
  4951. return hasEnvVariables ? process.env[key] : undefined;
  4952. }
  4953. function getNativePromise() {
  4954. if (typeof Promise === "function") {
  4955. try {
  4956. var promise = new Promise(function(){});
  4957. if ({}.toString.call(promise) === "[object Promise]") {
  4958. return Promise;
  4959. }
  4960. } catch (e) {}
  4961. }
  4962. }
  4963. function domainBind(self, cb) {
  4964. return self.bind(cb);
  4965. }
  4966. var ret = {
  4967. isClass: isClass,
  4968. isIdentifier: isIdentifier,
  4969. inheritedDataKeys: inheritedDataKeys,
  4970. getDataPropertyOrDefault: getDataPropertyOrDefault,
  4971. thrower: thrower,
  4972. isArray: es5.isArray,
  4973. asArray: asArray,
  4974. notEnumerableProp: notEnumerableProp,
  4975. isPrimitive: isPrimitive,
  4976. isObject: isObject,
  4977. isError: isError,
  4978. canEvaluate: canEvaluate,
  4979. errorObj: errorObj,
  4980. tryCatch: tryCatch,
  4981. inherits: inherits,
  4982. withAppended: withAppended,
  4983. maybeWrapAsError: maybeWrapAsError,
  4984. toFastProperties: toFastProperties,
  4985. filledRange: filledRange,
  4986. toString: safeToString,
  4987. canAttachTrace: canAttachTrace,
  4988. ensureErrorObject: ensureErrorObject,
  4989. originatesFromRejection: originatesFromRejection,
  4990. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  4991. classString: classString,
  4992. copyDescriptors: copyDescriptors,
  4993. hasDevTools: typeof chrome !== "undefined" && chrome &&
  4994. typeof chrome.loadTimes === "function",
  4995. isNode: isNode,
  4996. hasEnvVariables: hasEnvVariables,
  4997. env: env,
  4998. global: globalObject,
  4999. getNativePromise: getNativePromise,
  5000. domainBind: domainBind
  5001. };
  5002. ret.isRecentNode = ret.isNode && (function() {
  5003. var version = process.versions.node.split(".").map(Number);
  5004. return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
  5005. })();
  5006. if (ret.isNode) ret.toFastProperties(process);
  5007. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  5008. module.exports = ret;
  5009. },{"./es5":13}]},{},[4])(4)
  5010. }); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; }