bluebird.core.js 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  1. /* @preserve
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2013-2015 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.3.3
  27. * Features enabled: core
  28. * Features disabled: race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each
  29. */
  30. !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){
  31. "use strict";
  32. var firstLineError;
  33. try {throw new Error(); } catch (e) {firstLineError = e;}
  34. var schedule = _dereq_("./schedule");
  35. var Queue = _dereq_("./queue");
  36. var util = _dereq_("./util");
  37. function Async() {
  38. this._isTickUsed = false;
  39. this._lateQueue = new Queue(16);
  40. this._normalQueue = new Queue(16);
  41. this._haveDrainedQueues = false;
  42. this._trampolineEnabled = true;
  43. var self = this;
  44. this.drainQueues = function () {
  45. self._drainQueues();
  46. };
  47. this._schedule = schedule;
  48. }
  49. Async.prototype.enableTrampoline = function() {
  50. this._trampolineEnabled = true;
  51. };
  52. Async.prototype.disableTrampolineIfNecessary = function() {
  53. if (util.hasDevTools) {
  54. this._trampolineEnabled = false;
  55. }
  56. };
  57. Async.prototype.haveItemsQueued = function () {
  58. return this._isTickUsed || this._haveDrainedQueues;
  59. };
  60. Async.prototype.fatalError = function(e, isNode) {
  61. if (isNode) {
  62. process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e) +
  63. "\n");
  64. process.exit(2);
  65. } else {
  66. this.throwLater(e);
  67. }
  68. };
  69. Async.prototype.throwLater = function(fn, arg) {
  70. if (arguments.length === 1) {
  71. arg = fn;
  72. fn = function () { throw arg; };
  73. }
  74. if (typeof setTimeout !== "undefined") {
  75. setTimeout(function() {
  76. fn(arg);
  77. }, 0);
  78. } else try {
  79. this._schedule(function() {
  80. fn(arg);
  81. });
  82. } catch (e) {
  83. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  84. }
  85. };
  86. function AsyncInvokeLater(fn, receiver, arg) {
  87. this._lateQueue.push(fn, receiver, arg);
  88. this._queueTick();
  89. }
  90. function AsyncInvoke(fn, receiver, arg) {
  91. this._normalQueue.push(fn, receiver, arg);
  92. this._queueTick();
  93. }
  94. function AsyncSettlePromises(promise) {
  95. this._normalQueue._pushOne(promise);
  96. this._queueTick();
  97. }
  98. if (!util.hasDevTools) {
  99. Async.prototype.invokeLater = AsyncInvokeLater;
  100. Async.prototype.invoke = AsyncInvoke;
  101. Async.prototype.settlePromises = AsyncSettlePromises;
  102. } else {
  103. Async.prototype.invokeLater = function (fn, receiver, arg) {
  104. if (this._trampolineEnabled) {
  105. AsyncInvokeLater.call(this, fn, receiver, arg);
  106. } else {
  107. this._schedule(function() {
  108. setTimeout(function() {
  109. fn.call(receiver, arg);
  110. }, 100);
  111. });
  112. }
  113. };
  114. Async.prototype.invoke = function (fn, receiver, arg) {
  115. if (this._trampolineEnabled) {
  116. AsyncInvoke.call(this, fn, receiver, arg);
  117. } else {
  118. this._schedule(function() {
  119. fn.call(receiver, arg);
  120. });
  121. }
  122. };
  123. Async.prototype.settlePromises = function(promise) {
  124. if (this._trampolineEnabled) {
  125. AsyncSettlePromises.call(this, promise);
  126. } else {
  127. this._schedule(function() {
  128. promise._settlePromises();
  129. });
  130. }
  131. };
  132. }
  133. Async.prototype.invokeFirst = function (fn, receiver, arg) {
  134. this._normalQueue.unshift(fn, receiver, arg);
  135. this._queueTick();
  136. };
  137. Async.prototype._drainQueue = function(queue) {
  138. while (queue.length() > 0) {
  139. var fn = queue.shift();
  140. if (typeof fn !== "function") {
  141. fn._settlePromises();
  142. continue;
  143. }
  144. var receiver = queue.shift();
  145. var arg = queue.shift();
  146. fn.call(receiver, arg);
  147. }
  148. };
  149. Async.prototype._drainQueues = function () {
  150. this._drainQueue(this._normalQueue);
  151. this._reset();
  152. this._haveDrainedQueues = true;
  153. this._drainQueue(this._lateQueue);
  154. };
  155. Async.prototype._queueTick = function () {
  156. if (!this._isTickUsed) {
  157. this._isTickUsed = true;
  158. this._schedule(this.drainQueues);
  159. }
  160. };
  161. Async.prototype._reset = function () {
  162. this._isTickUsed = false;
  163. };
  164. module.exports = Async;
  165. module.exports.firstLineError = firstLineError;
  166. },{"./queue":17,"./schedule":18,"./util":21}],2:[function(_dereq_,module,exports){
  167. "use strict";
  168. module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
  169. var calledBind = false;
  170. var rejectThis = function(_, e) {
  171. this._reject(e);
  172. };
  173. var targetRejected = function(e, context) {
  174. context.promiseRejectionQueued = true;
  175. context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
  176. };
  177. var bindingResolved = function(thisArg, context) {
  178. if (((this._bitField & 50397184) === 0)) {
  179. this._resolveCallback(context.target);
  180. }
  181. };
  182. var bindingRejected = function(e, context) {
  183. if (!context.promiseRejectionQueued) this._reject(e);
  184. };
  185. Promise.prototype.bind = function (thisArg) {
  186. if (!calledBind) {
  187. calledBind = true;
  188. Promise.prototype._propagateFrom = debug.propagateFromFunction();
  189. Promise.prototype._boundValue = debug.boundValueFunction();
  190. }
  191. var maybePromise = tryConvertToPromise(thisArg);
  192. var ret = new Promise(INTERNAL);
  193. ret._propagateFrom(this, 1);
  194. var target = this._target();
  195. ret._setBoundTo(maybePromise);
  196. if (maybePromise instanceof Promise) {
  197. var context = {
  198. promiseRejectionQueued: false,
  199. promise: ret,
  200. target: target,
  201. bindingPromise: maybePromise
  202. };
  203. target._then(INTERNAL, targetRejected, undefined, ret, context);
  204. maybePromise._then(
  205. bindingResolved, bindingRejected, undefined, ret, context);
  206. ret._setOnCancel(maybePromise);
  207. } else {
  208. ret._resolveCallback(target);
  209. }
  210. return ret;
  211. };
  212. Promise.prototype._setBoundTo = function (obj) {
  213. if (obj !== undefined) {
  214. this._bitField = this._bitField | 2097152;
  215. this._boundTo = obj;
  216. } else {
  217. this._bitField = this._bitField & (~2097152);
  218. }
  219. };
  220. Promise.prototype._isBound = function () {
  221. return (this._bitField & 2097152) === 2097152;
  222. };
  223. Promise.bind = function (thisArg, value) {
  224. return Promise.resolve(value).bind(thisArg);
  225. };
  226. };
  227. },{}],3:[function(_dereq_,module,exports){
  228. "use strict";
  229. var old;
  230. if (typeof Promise !== "undefined") old = Promise;
  231. function noConflict() {
  232. try { if (Promise === bluebird) Promise = old; }
  233. catch (e) {}
  234. return bluebird;
  235. }
  236. var bluebird = _dereq_("./promise")();
  237. bluebird.noConflict = noConflict;
  238. module.exports = bluebird;
  239. },{"./promise":15}],4:[function(_dereq_,module,exports){
  240. "use strict";
  241. module.exports = function(Promise, PromiseArray, apiRejection, debug) {
  242. var util = _dereq_("./util");
  243. var tryCatch = util.tryCatch;
  244. var errorObj = util.errorObj;
  245. var async = Promise._async;
  246. Promise.prototype["break"] = Promise.prototype.cancel = function() {
  247. if (!debug.cancellation()) return this._warn("cancellation is disabled");
  248. var promise = this;
  249. var child = promise;
  250. while (promise.isCancellable()) {
  251. if (!promise._cancelBy(child)) {
  252. if (child._isFollowing()) {
  253. child._followee().cancel();
  254. } else {
  255. child._cancelBranched();
  256. }
  257. break;
  258. }
  259. var parent = promise._cancellationParent;
  260. if (parent == null || !parent.isCancellable()) {
  261. if (promise._isFollowing()) {
  262. promise._followee().cancel();
  263. } else {
  264. promise._cancelBranched();
  265. }
  266. break;
  267. } else {
  268. if (promise._isFollowing()) promise._followee().cancel();
  269. child = promise;
  270. promise = parent;
  271. }
  272. }
  273. };
  274. Promise.prototype._branchHasCancelled = function() {
  275. this._branchesRemainingToCancel--;
  276. };
  277. Promise.prototype._enoughBranchesHaveCancelled = function() {
  278. return this._branchesRemainingToCancel === undefined ||
  279. this._branchesRemainingToCancel <= 0;
  280. };
  281. Promise.prototype._cancelBy = function(canceller) {
  282. if (canceller === this) {
  283. this._branchesRemainingToCancel = 0;
  284. this._invokeOnCancel();
  285. return true;
  286. } else {
  287. this._branchHasCancelled();
  288. if (this._enoughBranchesHaveCancelled()) {
  289. this._invokeOnCancel();
  290. return true;
  291. }
  292. }
  293. return false;
  294. };
  295. Promise.prototype._cancelBranched = function() {
  296. if (this._enoughBranchesHaveCancelled()) {
  297. this._cancel();
  298. }
  299. };
  300. Promise.prototype._cancel = function() {
  301. if (!this.isCancellable()) return;
  302. this._setCancelled();
  303. async.invoke(this._cancelPromises, this, undefined);
  304. };
  305. Promise.prototype._cancelPromises = function() {
  306. if (this._length() > 0) this._settlePromises();
  307. };
  308. Promise.prototype._unsetOnCancel = function() {
  309. this._onCancelField = undefined;
  310. };
  311. Promise.prototype.isCancellable = function() {
  312. return this.isPending() && !this.isCancelled();
  313. };
  314. Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) {
  315. if (util.isArray(onCancelCallback)) {
  316. for (var i = 0; i < onCancelCallback.length; ++i) {
  317. this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
  318. }
  319. } else if (onCancelCallback !== undefined) {
  320. if (typeof onCancelCallback === "function") {
  321. if (!internalOnly) {
  322. var e = tryCatch(onCancelCallback).call(this._boundValue());
  323. if (e === errorObj) {
  324. this._attachExtraTrace(e.e);
  325. async.throwLater(e.e);
  326. }
  327. }
  328. } else {
  329. onCancelCallback._resultCancelled(this);
  330. }
  331. }
  332. };
  333. Promise.prototype._invokeOnCancel = function() {
  334. var onCancelCallback = this._onCancel();
  335. this._unsetOnCancel();
  336. async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
  337. };
  338. Promise.prototype._invokeInternalOnCancel = function() {
  339. if (this.isCancellable()) {
  340. this._doInvokeOnCancel(this._onCancel(), true);
  341. this._unsetOnCancel();
  342. }
  343. };
  344. Promise.prototype._resultCancelled = function() {
  345. this.cancel();
  346. };
  347. };
  348. },{"./util":21}],5:[function(_dereq_,module,exports){
  349. "use strict";
  350. module.exports = function(NEXT_FILTER) {
  351. var util = _dereq_("./util");
  352. var getKeys = _dereq_("./es5").keys;
  353. var tryCatch = util.tryCatch;
  354. var errorObj = util.errorObj;
  355. function catchFilter(instances, cb, promise) {
  356. return function(e) {
  357. var boundTo = promise._boundValue();
  358. predicateLoop: for (var i = 0; i < instances.length; ++i) {
  359. var item = instances[i];
  360. if (item === Error ||
  361. (item != null && item.prototype instanceof Error)) {
  362. if (e instanceof item) {
  363. return tryCatch(cb).call(boundTo, e);
  364. }
  365. } else if (typeof item === "function") {
  366. var matchesPredicate = tryCatch(item).call(boundTo, e);
  367. if (matchesPredicate === errorObj) {
  368. return matchesPredicate;
  369. } else if (matchesPredicate) {
  370. return tryCatch(cb).call(boundTo, e);
  371. }
  372. } else if (util.isObject(e)) {
  373. var keys = getKeys(item);
  374. for (var j = 0; j < keys.length; ++j) {
  375. var key = keys[j];
  376. if (item[key] != e[key]) {
  377. continue predicateLoop;
  378. }
  379. }
  380. return tryCatch(cb).call(boundTo, e);
  381. }
  382. }
  383. return NEXT_FILTER;
  384. };
  385. }
  386. return catchFilter;
  387. };
  388. },{"./es5":10,"./util":21}],6:[function(_dereq_,module,exports){
  389. "use strict";
  390. module.exports = function(Promise) {
  391. var longStackTraces = false;
  392. var contextStack = [];
  393. Promise.prototype._promiseCreated = function() {};
  394. Promise.prototype._pushContext = function() {};
  395. Promise.prototype._popContext = function() {return null;};
  396. Promise._peekContext = Promise.prototype._peekContext = function() {};
  397. function Context() {
  398. this._trace = new Context.CapturedTrace(peekContext());
  399. }
  400. Context.prototype._pushContext = function () {
  401. if (this._trace !== undefined) {
  402. this._trace._promiseCreated = null;
  403. contextStack.push(this._trace);
  404. }
  405. };
  406. Context.prototype._popContext = function () {
  407. if (this._trace !== undefined) {
  408. var trace = contextStack.pop();
  409. var ret = trace._promiseCreated;
  410. trace._promiseCreated = null;
  411. return ret;
  412. }
  413. return null;
  414. };
  415. function createContext() {
  416. if (longStackTraces) return new Context();
  417. }
  418. function peekContext() {
  419. var lastIndex = contextStack.length - 1;
  420. if (lastIndex >= 0) {
  421. return contextStack[lastIndex];
  422. }
  423. return undefined;
  424. }
  425. Context.CapturedTrace = null;
  426. Context.create = createContext;
  427. Context.deactivateLongStackTraces = function() {};
  428. Context.activateLongStackTraces = function() {
  429. var Promise_pushContext = Promise.prototype._pushContext;
  430. var Promise_popContext = Promise.prototype._popContext;
  431. var Promise_PeekContext = Promise._peekContext;
  432. var Promise_peekContext = Promise.prototype._peekContext;
  433. var Promise_promiseCreated = Promise.prototype._promiseCreated;
  434. Context.deactivateLongStackTraces = function() {
  435. Promise.prototype._pushContext = Promise_pushContext;
  436. Promise.prototype._popContext = Promise_popContext;
  437. Promise._peekContext = Promise_PeekContext;
  438. Promise.prototype._peekContext = Promise_peekContext;
  439. Promise.prototype._promiseCreated = Promise_promiseCreated;
  440. longStackTraces = false;
  441. };
  442. longStackTraces = true;
  443. Promise.prototype._pushContext = Context.prototype._pushContext;
  444. Promise.prototype._popContext = Context.prototype._popContext;
  445. Promise._peekContext = Promise.prototype._peekContext = peekContext;
  446. Promise.prototype._promiseCreated = function() {
  447. var ctx = this._peekContext();
  448. if (ctx && ctx._promiseCreated == null) ctx._promiseCreated = this;
  449. };
  450. };
  451. return Context;
  452. };
  453. },{}],7:[function(_dereq_,module,exports){
  454. "use strict";
  455. module.exports = function(Promise, Context) {
  456. var getDomain = Promise._getDomain;
  457. var async = Promise._async;
  458. var Warning = _dereq_("./errors").Warning;
  459. var util = _dereq_("./util");
  460. var canAttachTrace = util.canAttachTrace;
  461. var unhandledRejectionHandled;
  462. var possiblyUnhandledRejection;
  463. var bluebirdFramePattern =
  464. /[\\\/]bluebird[\\\/]js[\\\/](release|debug|instrumented)/;
  465. var stackFramePattern = null;
  466. var formatStack = null;
  467. var indentStackFrames = false;
  468. var printWarning;
  469. var debugging = !!(util.env("BLUEBIRD_DEBUG") != 0 &&
  470. (true ||
  471. util.env("BLUEBIRD_DEBUG") ||
  472. util.env("NODE_ENV") === "development"));
  473. var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 &&
  474. (debugging || util.env("BLUEBIRD_WARNINGS")));
  475. var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
  476. (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));
  477. var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
  478. (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
  479. Promise.prototype.suppressUnhandledRejections = function() {
  480. var target = this._target();
  481. target._bitField = ((target._bitField & (~1048576)) |
  482. 524288);
  483. };
  484. Promise.prototype._ensurePossibleRejectionHandled = function () {
  485. if ((this._bitField & 524288) !== 0) return;
  486. this._setRejectionIsUnhandled();
  487. async.invokeLater(this._notifyUnhandledRejection, this, undefined);
  488. };
  489. Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
  490. fireRejectionEvent("rejectionHandled",
  491. unhandledRejectionHandled, undefined, this);
  492. };
  493. Promise.prototype._setReturnedNonUndefined = function() {
  494. this._bitField = this._bitField | 268435456;
  495. };
  496. Promise.prototype._returnedNonUndefined = function() {
  497. return (this._bitField & 268435456) !== 0;
  498. };
  499. Promise.prototype._notifyUnhandledRejection = function () {
  500. if (this._isRejectionUnhandled()) {
  501. var reason = this._settledValue();
  502. this._setUnhandledRejectionIsNotified();
  503. fireRejectionEvent("unhandledRejection",
  504. possiblyUnhandledRejection, reason, this);
  505. }
  506. };
  507. Promise.prototype._setUnhandledRejectionIsNotified = function () {
  508. this._bitField = this._bitField | 262144;
  509. };
  510. Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
  511. this._bitField = this._bitField & (~262144);
  512. };
  513. Promise.prototype._isUnhandledRejectionNotified = function () {
  514. return (this._bitField & 262144) > 0;
  515. };
  516. Promise.prototype._setRejectionIsUnhandled = function () {
  517. this._bitField = this._bitField | 1048576;
  518. };
  519. Promise.prototype._unsetRejectionIsUnhandled = function () {
  520. this._bitField = this._bitField & (~1048576);
  521. if (this._isUnhandledRejectionNotified()) {
  522. this._unsetUnhandledRejectionIsNotified();
  523. this._notifyUnhandledRejectionIsHandled();
  524. }
  525. };
  526. Promise.prototype._isRejectionUnhandled = function () {
  527. return (this._bitField & 1048576) > 0;
  528. };
  529. Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) {
  530. return warn(message, shouldUseOwnTrace, promise || this);
  531. };
  532. Promise.onPossiblyUnhandledRejection = function (fn) {
  533. var domain = getDomain();
  534. possiblyUnhandledRejection =
  535. typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
  536. : undefined;
  537. };
  538. Promise.onUnhandledRejectionHandled = function (fn) {
  539. var domain = getDomain();
  540. unhandledRejectionHandled =
  541. typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
  542. : undefined;
  543. };
  544. var disableLongStackTraces = function() {};
  545. Promise.longStackTraces = function () {
  546. if (async.haveItemsQueued() && !config.longStackTraces) {
  547. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  548. }
  549. if (!config.longStackTraces && longStackTracesIsSupported()) {
  550. var Promise_captureStackTrace = Promise.prototype._captureStackTrace;
  551. var Promise_attachExtraTrace = Promise.prototype._attachExtraTrace;
  552. config.longStackTraces = true;
  553. disableLongStackTraces = function() {
  554. if (async.haveItemsQueued() && !config.longStackTraces) {
  555. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  556. }
  557. Promise.prototype._captureStackTrace = Promise_captureStackTrace;
  558. Promise.prototype._attachExtraTrace = Promise_attachExtraTrace;
  559. Context.deactivateLongStackTraces();
  560. async.enableTrampoline();
  561. config.longStackTraces = false;
  562. };
  563. Promise.prototype._captureStackTrace = longStackTracesCaptureStackTrace;
  564. Promise.prototype._attachExtraTrace = longStackTracesAttachExtraTrace;
  565. Context.activateLongStackTraces();
  566. async.disableTrampolineIfNecessary();
  567. }
  568. };
  569. Promise.hasLongStackTraces = function () {
  570. return config.longStackTraces && longStackTracesIsSupported();
  571. };
  572. var fireDomEvent = (function() {
  573. try {
  574. var event = document.createEvent("CustomEvent");
  575. event.initCustomEvent("testingtheevent", false, true, {});
  576. util.global.dispatchEvent(event);
  577. return function(name, event) {
  578. var domEvent = document.createEvent("CustomEvent");
  579. domEvent.initCustomEvent(name.toLowerCase(), false, true, event);
  580. return !util.global.dispatchEvent(domEvent);
  581. };
  582. } catch (e) {}
  583. return function() {
  584. return false;
  585. };
  586. })();
  587. var fireGlobalEvent = (function() {
  588. if (util.isNode) {
  589. return function() {
  590. return process.emit.apply(process, arguments);
  591. };
  592. } else {
  593. if (!util.global) {
  594. return function() {
  595. return false;
  596. };
  597. }
  598. return function(name) {
  599. var methodName = "on" + name.toLowerCase();
  600. var method = util.global[methodName];
  601. if (!method) return false;
  602. method.apply(util.global, [].slice.call(arguments, 1));
  603. return true;
  604. };
  605. }
  606. })();
  607. function generatePromiseLifecycleEventObject(name, promise) {
  608. return {promise: promise};
  609. }
  610. var eventToObjectGenerator = {
  611. promiseCreated: generatePromiseLifecycleEventObject,
  612. promiseFulfilled: generatePromiseLifecycleEventObject,
  613. promiseRejected: generatePromiseLifecycleEventObject,
  614. promiseResolved: generatePromiseLifecycleEventObject,
  615. promiseCancelled: generatePromiseLifecycleEventObject,
  616. promiseChained: function(name, promise, child) {
  617. return {promise: promise, child: child};
  618. },
  619. warning: function(name, warning) {
  620. return {warning: warning};
  621. },
  622. unhandledRejection: function (name, reason, promise) {
  623. return {reason: reason, promise: promise};
  624. },
  625. rejectionHandled: generatePromiseLifecycleEventObject
  626. };
  627. var activeFireEvent = function (name) {
  628. var globalEventFired = false;
  629. try {
  630. globalEventFired = fireGlobalEvent.apply(null, arguments);
  631. } catch (e) {
  632. async.throwLater(e);
  633. globalEventFired = true;
  634. }
  635. var domEventFired = false;
  636. try {
  637. domEventFired = fireDomEvent(name,
  638. eventToObjectGenerator[name].apply(null, arguments));
  639. } catch (e) {
  640. async.throwLater(e);
  641. domEventFired = true;
  642. }
  643. return domEventFired || globalEventFired;
  644. };
  645. Promise.config = function(opts) {
  646. opts = Object(opts);
  647. if ("longStackTraces" in opts) {
  648. if (opts.longStackTraces) {
  649. Promise.longStackTraces();
  650. } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {
  651. disableLongStackTraces();
  652. }
  653. }
  654. if ("warnings" in opts) {
  655. var warningsOption = opts.warnings;
  656. config.warnings = !!warningsOption;
  657. wForgottenReturn = config.warnings;
  658. if (util.isObject(warningsOption)) {
  659. if ("wForgottenReturn" in warningsOption) {
  660. wForgottenReturn = !!warningsOption.wForgottenReturn;
  661. }
  662. }
  663. }
  664. if ("cancellation" in opts && opts.cancellation && !config.cancellation) {
  665. if (async.haveItemsQueued()) {
  666. throw new Error(
  667. "cannot enable cancellation after promises are in use");
  668. }
  669. Promise.prototype._clearCancellationData =
  670. cancellationClearCancellationData;
  671. Promise.prototype._propagateFrom = cancellationPropagateFrom;
  672. Promise.prototype._onCancel = cancellationOnCancel;
  673. Promise.prototype._setOnCancel = cancellationSetOnCancel;
  674. Promise.prototype._attachCancellationCallback =
  675. cancellationAttachCancellationCallback;
  676. Promise.prototype._execute = cancellationExecute;
  677. propagateFromFunction = cancellationPropagateFrom;
  678. config.cancellation = true;
  679. }
  680. if ("monitoring" in opts) {
  681. if (opts.monitoring && !config.monitoring) {
  682. config.monitoring = true;
  683. Promise.prototype._fireEvent = activeFireEvent;
  684. } else if (!opts.monitoring && config.monitoring) {
  685. config.monitoring = false;
  686. Promise.prototype._fireEvent = defaultFireEvent;
  687. }
  688. }
  689. };
  690. function defaultFireEvent() { return false; }
  691. Promise.prototype._fireEvent = defaultFireEvent;
  692. Promise.prototype._execute = function(executor, resolve, reject) {
  693. try {
  694. executor(resolve, reject);
  695. } catch (e) {
  696. return e;
  697. }
  698. };
  699. Promise.prototype._onCancel = function () {};
  700. Promise.prototype._setOnCancel = function (handler) { ; };
  701. Promise.prototype._attachCancellationCallback = function(onCancel) {
  702. ;
  703. };
  704. Promise.prototype._captureStackTrace = function () {};
  705. Promise.prototype._attachExtraTrace = function () {};
  706. Promise.prototype._clearCancellationData = function() {};
  707. Promise.prototype._propagateFrom = function (parent, flags) {
  708. ;
  709. ;
  710. };
  711. function cancellationExecute(executor, resolve, reject) {
  712. var promise = this;
  713. try {
  714. executor(resolve, reject, function(onCancel) {
  715. if (typeof onCancel !== "function") {
  716. throw new TypeError("onCancel must be a function, got: " +
  717. util.toString(onCancel));
  718. }
  719. promise._attachCancellationCallback(onCancel);
  720. });
  721. } catch (e) {
  722. return e;
  723. }
  724. }
  725. function cancellationAttachCancellationCallback(onCancel) {
  726. if (!this.isCancellable()) return this;
  727. var previousOnCancel = this._onCancel();
  728. if (previousOnCancel !== undefined) {
  729. if (util.isArray(previousOnCancel)) {
  730. previousOnCancel.push(onCancel);
  731. } else {
  732. this._setOnCancel([previousOnCancel, onCancel]);
  733. }
  734. } else {
  735. this._setOnCancel(onCancel);
  736. }
  737. }
  738. function cancellationOnCancel() {
  739. return this._onCancelField;
  740. }
  741. function cancellationSetOnCancel(onCancel) {
  742. this._onCancelField = onCancel;
  743. }
  744. function cancellationClearCancellationData() {
  745. this._cancellationParent = undefined;
  746. this._onCancelField = undefined;
  747. }
  748. function cancellationPropagateFrom(parent, flags) {
  749. if ((flags & 1) !== 0) {
  750. this._cancellationParent = parent;
  751. var branchesRemainingToCancel = parent._branchesRemainingToCancel;
  752. if (branchesRemainingToCancel === undefined) {
  753. branchesRemainingToCancel = 0;
  754. }
  755. parent._branchesRemainingToCancel = branchesRemainingToCancel + 1;
  756. }
  757. if ((flags & 2) !== 0 && parent._isBound()) {
  758. this._setBoundTo(parent._boundTo);
  759. }
  760. }
  761. function bindingPropagateFrom(parent, flags) {
  762. if ((flags & 2) !== 0 && parent._isBound()) {
  763. this._setBoundTo(parent._boundTo);
  764. }
  765. }
  766. var propagateFromFunction = bindingPropagateFrom;
  767. function boundValueFunction() {
  768. var ret = this._boundTo;
  769. if (ret !== undefined) {
  770. if (ret instanceof Promise) {
  771. if (ret.isFulfilled()) {
  772. return ret.value();
  773. } else {
  774. return undefined;
  775. }
  776. }
  777. }
  778. return ret;
  779. }
  780. function longStackTracesCaptureStackTrace() {
  781. this._trace = new CapturedTrace(this._peekContext());
  782. }
  783. function longStackTracesAttachExtraTrace(error, ignoreSelf) {
  784. if (canAttachTrace(error)) {
  785. var trace = this._trace;
  786. if (trace !== undefined) {
  787. if (ignoreSelf) trace = trace._parent;
  788. }
  789. if (trace !== undefined) {
  790. trace.attachExtraTrace(error);
  791. } else if (!error.__stackCleaned__) {
  792. var parsed = parseStackAndMessage(error);
  793. util.notEnumerableProp(error, "stack",
  794. parsed.message + "\n" + parsed.stack.join("\n"));
  795. util.notEnumerableProp(error, "__stackCleaned__", true);
  796. }
  797. }
  798. }
  799. function checkForgottenReturns(returnValue, promiseCreated, name, promise,
  800. parent) {
  801. if (returnValue === undefined && promiseCreated !== null &&
  802. wForgottenReturn) {
  803. if (parent !== undefined && parent._returnedNonUndefined()) return;
  804. if (name) name = name + " ";
  805. var msg = "a promise was created in a " + name +
  806. "handler but was not returned from it";
  807. promise._warn(msg, true, promiseCreated);
  808. }
  809. }
  810. function deprecated(name, replacement) {
  811. var message = name +
  812. " is deprecated and will be removed in a future version.";
  813. if (replacement) message += " Use " + replacement + " instead.";
  814. return warn(message);
  815. }
  816. function warn(message, shouldUseOwnTrace, promise) {
  817. if (!config.warnings) return;
  818. var warning = new Warning(message);
  819. var ctx;
  820. if (shouldUseOwnTrace) {
  821. promise._attachExtraTrace(warning);
  822. } else if (config.longStackTraces && (ctx = Promise._peekContext())) {
  823. ctx.attachExtraTrace(warning);
  824. } else {
  825. var parsed = parseStackAndMessage(warning);
  826. warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
  827. }
  828. if (!activeFireEvent("warning", warning)) {
  829. formatAndLogError(warning, "", true);
  830. }
  831. }
  832. function reconstructStack(message, stacks) {
  833. for (var i = 0; i < stacks.length - 1; ++i) {
  834. stacks[i].push("From previous event:");
  835. stacks[i] = stacks[i].join("\n");
  836. }
  837. if (i < stacks.length) {
  838. stacks[i] = stacks[i].join("\n");
  839. }
  840. return message + "\n" + stacks.join("\n");
  841. }
  842. function removeDuplicateOrEmptyJumps(stacks) {
  843. for (var i = 0; i < stacks.length; ++i) {
  844. if (stacks[i].length === 0 ||
  845. ((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) {
  846. stacks.splice(i, 1);
  847. i--;
  848. }
  849. }
  850. }
  851. function removeCommonRoots(stacks) {
  852. var current = stacks[0];
  853. for (var i = 1; i < stacks.length; ++i) {
  854. var prev = stacks[i];
  855. var currentLastIndex = current.length - 1;
  856. var currentLastLine = current[currentLastIndex];
  857. var commonRootMeetPoint = -1;
  858. for (var j = prev.length - 1; j >= 0; --j) {
  859. if (prev[j] === currentLastLine) {
  860. commonRootMeetPoint = j;
  861. break;
  862. }
  863. }
  864. for (var j = commonRootMeetPoint; j >= 0; --j) {
  865. var line = prev[j];
  866. if (current[currentLastIndex] === line) {
  867. current.pop();
  868. currentLastIndex--;
  869. } else {
  870. break;
  871. }
  872. }
  873. current = prev;
  874. }
  875. }
  876. function cleanStack(stack) {
  877. var ret = [];
  878. for (var i = 0; i < stack.length; ++i) {
  879. var line = stack[i];
  880. var isTraceLine = " (No stack trace)" === line ||
  881. stackFramePattern.test(line);
  882. var isInternalFrame = isTraceLine && shouldIgnore(line);
  883. if (isTraceLine && !isInternalFrame) {
  884. if (indentStackFrames && line.charAt(0) !== " ") {
  885. line = " " + line;
  886. }
  887. ret.push(line);
  888. }
  889. }
  890. return ret;
  891. }
  892. function stackFramesAsArray(error) {
  893. var stack = error.stack.replace(/\s+$/g, "").split("\n");
  894. for (var i = 0; i < stack.length; ++i) {
  895. var line = stack[i];
  896. if (" (No stack trace)" === line || stackFramePattern.test(line)) {
  897. break;
  898. }
  899. }
  900. if (i > 0) {
  901. stack = stack.slice(i);
  902. }
  903. return stack;
  904. }
  905. function parseStackAndMessage(error) {
  906. var stack = error.stack;
  907. var message = error.toString();
  908. stack = typeof stack === "string" && stack.length > 0
  909. ? stackFramesAsArray(error) : [" (No stack trace)"];
  910. return {
  911. message: message,
  912. stack: cleanStack(stack)
  913. };
  914. }
  915. function formatAndLogError(error, title, isSoft) {
  916. if (typeof console !== "undefined") {
  917. var message;
  918. if (util.isObject(error)) {
  919. var stack = error.stack;
  920. message = title + formatStack(stack, error);
  921. } else {
  922. message = title + String(error);
  923. }
  924. if (typeof printWarning === "function") {
  925. printWarning(message, isSoft);
  926. } else if (typeof console.log === "function" ||
  927. typeof console.log === "object") {
  928. console.log(message);
  929. }
  930. }
  931. }
  932. function fireRejectionEvent(name, localHandler, reason, promise) {
  933. var localEventFired = false;
  934. try {
  935. if (typeof localHandler === "function") {
  936. localEventFired = true;
  937. if (name === "rejectionHandled") {
  938. localHandler(promise);
  939. } else {
  940. localHandler(reason, promise);
  941. }
  942. }
  943. } catch (e) {
  944. async.throwLater(e);
  945. }
  946. if (name === "unhandledRejection") {
  947. if (!activeFireEvent(name, reason, promise) && !localEventFired) {
  948. formatAndLogError(reason, "Unhandled rejection ");
  949. }
  950. } else {
  951. activeFireEvent(name, promise);
  952. }
  953. }
  954. function formatNonError(obj) {
  955. var str;
  956. if (typeof obj === "function") {
  957. str = "[function " +
  958. (obj.name || "anonymous") +
  959. "]";
  960. } else {
  961. str = obj && typeof obj.toString === "function"
  962. ? obj.toString() : util.toString(obj);
  963. var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
  964. if (ruselessToString.test(str)) {
  965. try {
  966. var newStr = JSON.stringify(obj);
  967. str = newStr;
  968. }
  969. catch(e) {
  970. }
  971. }
  972. if (str.length === 0) {
  973. str = "(empty array)";
  974. }
  975. }
  976. return ("(<" + snip(str) + ">, no stack trace)");
  977. }
  978. function snip(str) {
  979. var maxChars = 41;
  980. if (str.length < maxChars) {
  981. return str;
  982. }
  983. return str.substr(0, maxChars - 3) + "...";
  984. }
  985. function longStackTracesIsSupported() {
  986. return typeof captureStackTrace === "function";
  987. }
  988. var shouldIgnore = function() { return false; };
  989. var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/;
  990. function parseLineInfo(line) {
  991. var matches = line.match(parseLineInfoRegex);
  992. if (matches) {
  993. return {
  994. fileName: matches[1],
  995. line: parseInt(matches[2], 10)
  996. };
  997. }
  998. }
  999. function setBounds(firstLineError, lastLineError) {
  1000. if (!longStackTracesIsSupported()) return;
  1001. var firstStackLines = firstLineError.stack.split("\n");
  1002. var lastStackLines = lastLineError.stack.split("\n");
  1003. var firstIndex = -1;
  1004. var lastIndex = -1;
  1005. var firstFileName;
  1006. var lastFileName;
  1007. for (var i = 0; i < firstStackLines.length; ++i) {
  1008. var result = parseLineInfo(firstStackLines[i]);
  1009. if (result) {
  1010. firstFileName = result.fileName;
  1011. firstIndex = result.line;
  1012. break;
  1013. }
  1014. }
  1015. for (var i = 0; i < lastStackLines.length; ++i) {
  1016. var result = parseLineInfo(lastStackLines[i]);
  1017. if (result) {
  1018. lastFileName = result.fileName;
  1019. lastIndex = result.line;
  1020. break;
  1021. }
  1022. }
  1023. if (firstIndex < 0 || lastIndex < 0 || !firstFileName || !lastFileName ||
  1024. firstFileName !== lastFileName || firstIndex >= lastIndex) {
  1025. return;
  1026. }
  1027. shouldIgnore = function(line) {
  1028. if (bluebirdFramePattern.test(line)) return true;
  1029. var info = parseLineInfo(line);
  1030. if (info) {
  1031. if (info.fileName === firstFileName &&
  1032. (firstIndex <= info.line && info.line <= lastIndex)) {
  1033. return true;
  1034. }
  1035. }
  1036. return false;
  1037. };
  1038. }
  1039. function CapturedTrace(parent) {
  1040. this._parent = parent;
  1041. this._promisesCreated = 0;
  1042. var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
  1043. captureStackTrace(this, CapturedTrace);
  1044. if (length > 32) this.uncycle();
  1045. }
  1046. util.inherits(CapturedTrace, Error);
  1047. Context.CapturedTrace = CapturedTrace;
  1048. CapturedTrace.prototype.uncycle = function() {
  1049. var length = this._length;
  1050. if (length < 2) return;
  1051. var nodes = [];
  1052. var stackToIndex = {};
  1053. for (var i = 0, node = this; node !== undefined; ++i) {
  1054. nodes.push(node);
  1055. node = node._parent;
  1056. }
  1057. length = this._length = i;
  1058. for (var i = length - 1; i >= 0; --i) {
  1059. var stack = nodes[i].stack;
  1060. if (stackToIndex[stack] === undefined) {
  1061. stackToIndex[stack] = i;
  1062. }
  1063. }
  1064. for (var i = 0; i < length; ++i) {
  1065. var currentStack = nodes[i].stack;
  1066. var index = stackToIndex[currentStack];
  1067. if (index !== undefined && index !== i) {
  1068. if (index > 0) {
  1069. nodes[index - 1]._parent = undefined;
  1070. nodes[index - 1]._length = 1;
  1071. }
  1072. nodes[i]._parent = undefined;
  1073. nodes[i]._length = 1;
  1074. var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
  1075. if (index < length - 1) {
  1076. cycleEdgeNode._parent = nodes[index + 1];
  1077. cycleEdgeNode._parent.uncycle();
  1078. cycleEdgeNode._length =
  1079. cycleEdgeNode._parent._length + 1;
  1080. } else {
  1081. cycleEdgeNode._parent = undefined;
  1082. cycleEdgeNode._length = 1;
  1083. }
  1084. var currentChildLength = cycleEdgeNode._length + 1;
  1085. for (var j = i - 2; j >= 0; --j) {
  1086. nodes[j]._length = currentChildLength;
  1087. currentChildLength++;
  1088. }
  1089. return;
  1090. }
  1091. }
  1092. };
  1093. CapturedTrace.prototype.attachExtraTrace = function(error) {
  1094. if (error.__stackCleaned__) return;
  1095. this.uncycle();
  1096. var parsed = parseStackAndMessage(error);
  1097. var message = parsed.message;
  1098. var stacks = [parsed.stack];
  1099. var trace = this;
  1100. while (trace !== undefined) {
  1101. stacks.push(cleanStack(trace.stack.split("\n")));
  1102. trace = trace._parent;
  1103. }
  1104. removeCommonRoots(stacks);
  1105. removeDuplicateOrEmptyJumps(stacks);
  1106. util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
  1107. util.notEnumerableProp(error, "__stackCleaned__", true);
  1108. };
  1109. var captureStackTrace = (function stackDetection() {
  1110. var v8stackFramePattern = /^\s*at\s*/;
  1111. var v8stackFormatter = function(stack, error) {
  1112. if (typeof stack === "string") return stack;
  1113. if (error.name !== undefined &&
  1114. error.message !== undefined) {
  1115. return error.toString();
  1116. }
  1117. return formatNonError(error);
  1118. };
  1119. if (typeof Error.stackTraceLimit === "number" &&
  1120. typeof Error.captureStackTrace === "function") {
  1121. Error.stackTraceLimit += 6;
  1122. stackFramePattern = v8stackFramePattern;
  1123. formatStack = v8stackFormatter;
  1124. var captureStackTrace = Error.captureStackTrace;
  1125. shouldIgnore = function(line) {
  1126. return bluebirdFramePattern.test(line);
  1127. };
  1128. return function(receiver, ignoreUntil) {
  1129. Error.stackTraceLimit += 6;
  1130. captureStackTrace(receiver, ignoreUntil);
  1131. Error.stackTraceLimit -= 6;
  1132. };
  1133. }
  1134. var err = new Error();
  1135. if (typeof err.stack === "string" &&
  1136. err.stack.split("\n")[0].indexOf("stackDetection@") >= 0) {
  1137. stackFramePattern = /@/;
  1138. formatStack = v8stackFormatter;
  1139. indentStackFrames = true;
  1140. return function captureStackTrace(o) {
  1141. o.stack = new Error().stack;
  1142. };
  1143. }
  1144. var hasStackAfterThrow;
  1145. try { throw new Error(); }
  1146. catch(e) {
  1147. hasStackAfterThrow = ("stack" in e);
  1148. }
  1149. if (!("stack" in err) && hasStackAfterThrow &&
  1150. typeof Error.stackTraceLimit === "number") {
  1151. stackFramePattern = v8stackFramePattern;
  1152. formatStack = v8stackFormatter;
  1153. return function captureStackTrace(o) {
  1154. Error.stackTraceLimit += 6;
  1155. try { throw new Error(); }
  1156. catch(e) { o.stack = e.stack; }
  1157. Error.stackTraceLimit -= 6;
  1158. };
  1159. }
  1160. formatStack = function(stack, error) {
  1161. if (typeof stack === "string") return stack;
  1162. if ((typeof error === "object" ||
  1163. typeof error === "function") &&
  1164. error.name !== undefined &&
  1165. error.message !== undefined) {
  1166. return error.toString();
  1167. }
  1168. return formatNonError(error);
  1169. };
  1170. return null;
  1171. })([]);
  1172. if (typeof console !== "undefined" && typeof console.warn !== "undefined") {
  1173. printWarning = function (message) {
  1174. console.warn(message);
  1175. };
  1176. if (util.isNode && process.stderr.isTTY) {
  1177. printWarning = function(message, isSoft) {
  1178. var color = isSoft ? "\u001b[33m" : "\u001b[31m";
  1179. console.warn(color + message + "\u001b[0m\n");
  1180. };
  1181. } else if (!util.isNode && typeof (new Error().stack) === "string") {
  1182. printWarning = function(message, isSoft) {
  1183. console.warn("%c" + message,
  1184. isSoft ? "color: darkorange" : "color: red");
  1185. };
  1186. }
  1187. }
  1188. var config = {
  1189. warnings: warnings,
  1190. longStackTraces: false,
  1191. cancellation: false,
  1192. monitoring: false
  1193. };
  1194. if (longStackTraces) Promise.longStackTraces();
  1195. return {
  1196. longStackTraces: function() {
  1197. return config.longStackTraces;
  1198. },
  1199. warnings: function() {
  1200. return config.warnings;
  1201. },
  1202. cancellation: function() {
  1203. return config.cancellation;
  1204. },
  1205. monitoring: function() {
  1206. return config.monitoring;
  1207. },
  1208. propagateFromFunction: function() {
  1209. return propagateFromFunction;
  1210. },
  1211. boundValueFunction: function() {
  1212. return boundValueFunction;
  1213. },
  1214. checkForgottenReturns: checkForgottenReturns,
  1215. setBounds: setBounds,
  1216. warn: warn,
  1217. deprecated: deprecated,
  1218. CapturedTrace: CapturedTrace,
  1219. fireDomEvent: fireDomEvent,
  1220. fireGlobalEvent: fireGlobalEvent
  1221. };
  1222. };
  1223. },{"./errors":9,"./util":21}],8:[function(_dereq_,module,exports){
  1224. "use strict";
  1225. module.exports = function(Promise) {
  1226. function returner() {
  1227. return this.value;
  1228. }
  1229. function thrower() {
  1230. throw this.reason;
  1231. }
  1232. Promise.prototype["return"] =
  1233. Promise.prototype.thenReturn = function (value) {
  1234. if (value instanceof Promise) value.suppressUnhandledRejections();
  1235. return this._then(
  1236. returner, undefined, undefined, {value: value}, undefined);
  1237. };
  1238. Promise.prototype["throw"] =
  1239. Promise.prototype.thenThrow = function (reason) {
  1240. return this._then(
  1241. thrower, undefined, undefined, {reason: reason}, undefined);
  1242. };
  1243. Promise.prototype.catchThrow = function (reason) {
  1244. if (arguments.length <= 1) {
  1245. return this._then(
  1246. undefined, thrower, undefined, {reason: reason}, undefined);
  1247. } else {
  1248. var _reason = arguments[1];
  1249. var handler = function() {throw _reason;};
  1250. return this.caught(reason, handler);
  1251. }
  1252. };
  1253. Promise.prototype.catchReturn = function (value) {
  1254. if (arguments.length <= 1) {
  1255. if (value instanceof Promise) value.suppressUnhandledRejections();
  1256. return this._then(
  1257. undefined, returner, undefined, {value: value}, undefined);
  1258. } else {
  1259. var _value = arguments[1];
  1260. if (_value instanceof Promise) _value.suppressUnhandledRejections();
  1261. var handler = function() {return _value;};
  1262. return this.caught(value, handler);
  1263. }
  1264. };
  1265. };
  1266. },{}],9:[function(_dereq_,module,exports){
  1267. "use strict";
  1268. var es5 = _dereq_("./es5");
  1269. var Objectfreeze = es5.freeze;
  1270. var util = _dereq_("./util");
  1271. var inherits = util.inherits;
  1272. var notEnumerableProp = util.notEnumerableProp;
  1273. function subError(nameProperty, defaultMessage) {
  1274. function SubError(message) {
  1275. if (!(this instanceof SubError)) return new SubError(message);
  1276. notEnumerableProp(this, "message",
  1277. typeof message === "string" ? message : defaultMessage);
  1278. notEnumerableProp(this, "name", nameProperty);
  1279. if (Error.captureStackTrace) {
  1280. Error.captureStackTrace(this, this.constructor);
  1281. } else {
  1282. Error.call(this);
  1283. }
  1284. }
  1285. inherits(SubError, Error);
  1286. return SubError;
  1287. }
  1288. var _TypeError, _RangeError;
  1289. var Warning = subError("Warning", "warning");
  1290. var CancellationError = subError("CancellationError", "cancellation error");
  1291. var TimeoutError = subError("TimeoutError", "timeout error");
  1292. var AggregateError = subError("AggregateError", "aggregate error");
  1293. try {
  1294. _TypeError = TypeError;
  1295. _RangeError = RangeError;
  1296. } catch(e) {
  1297. _TypeError = subError("TypeError", "type error");
  1298. _RangeError = subError("RangeError", "range error");
  1299. }
  1300. var methods = ("join pop push shift unshift slice filter forEach some " +
  1301. "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" ");
  1302. for (var i = 0; i < methods.length; ++i) {
  1303. if (typeof Array.prototype[methods[i]] === "function") {
  1304. AggregateError.prototype[methods[i]] = Array.prototype[methods[i]];
  1305. }
  1306. }
  1307. es5.defineProperty(AggregateError.prototype, "length", {
  1308. value: 0,
  1309. configurable: false,
  1310. writable: true,
  1311. enumerable: true
  1312. });
  1313. AggregateError.prototype["isOperational"] = true;
  1314. var level = 0;
  1315. AggregateError.prototype.toString = function() {
  1316. var indent = Array(level * 4 + 1).join(" ");
  1317. var ret = "\n" + indent + "AggregateError of:" + "\n";
  1318. level++;
  1319. indent = Array(level * 4 + 1).join(" ");
  1320. for (var i = 0; i < this.length; ++i) {
  1321. var str = this[i] === this ? "[Circular AggregateError]" : this[i] + "";
  1322. var lines = str.split("\n");
  1323. for (var j = 0; j < lines.length; ++j) {
  1324. lines[j] = indent + lines[j];
  1325. }
  1326. str = lines.join("\n");
  1327. ret += str + "\n";
  1328. }
  1329. level--;
  1330. return ret;
  1331. };
  1332. function OperationalError(message) {
  1333. if (!(this instanceof OperationalError))
  1334. return new OperationalError(message);
  1335. notEnumerableProp(this, "name", "OperationalError");
  1336. notEnumerableProp(this, "message", message);
  1337. this.cause = message;
  1338. this["isOperational"] = true;
  1339. if (message instanceof Error) {
  1340. notEnumerableProp(this, "message", message.message);
  1341. notEnumerableProp(this, "stack", message.stack);
  1342. } else if (Error.captureStackTrace) {
  1343. Error.captureStackTrace(this, this.constructor);
  1344. }
  1345. }
  1346. inherits(OperationalError, Error);
  1347. var errorTypes = Error["__BluebirdErrorTypes__"];
  1348. if (!errorTypes) {
  1349. errorTypes = Objectfreeze({
  1350. CancellationError: CancellationError,
  1351. TimeoutError: TimeoutError,
  1352. OperationalError: OperationalError,
  1353. RejectionError: OperationalError,
  1354. AggregateError: AggregateError
  1355. });
  1356. es5.defineProperty(Error, "__BluebirdErrorTypes__", {
  1357. value: errorTypes,
  1358. writable: false,
  1359. enumerable: false,
  1360. configurable: false
  1361. });
  1362. }
  1363. module.exports = {
  1364. Error: Error,
  1365. TypeError: _TypeError,
  1366. RangeError: _RangeError,
  1367. CancellationError: errorTypes.CancellationError,
  1368. OperationalError: errorTypes.OperationalError,
  1369. TimeoutError: errorTypes.TimeoutError,
  1370. AggregateError: errorTypes.AggregateError,
  1371. Warning: Warning
  1372. };
  1373. },{"./es5":10,"./util":21}],10:[function(_dereq_,module,exports){
  1374. var isES5 = (function(){
  1375. "use strict";
  1376. return this === undefined;
  1377. })();
  1378. if (isES5) {
  1379. module.exports = {
  1380. freeze: Object.freeze,
  1381. defineProperty: Object.defineProperty,
  1382. getDescriptor: Object.getOwnPropertyDescriptor,
  1383. keys: Object.keys,
  1384. names: Object.getOwnPropertyNames,
  1385. getPrototypeOf: Object.getPrototypeOf,
  1386. isArray: Array.isArray,
  1387. isES5: isES5,
  1388. propertyIsWritable: function(obj, prop) {
  1389. var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
  1390. return !!(!descriptor || descriptor.writable || descriptor.set);
  1391. }
  1392. };
  1393. } else {
  1394. var has = {}.hasOwnProperty;
  1395. var str = {}.toString;
  1396. var proto = {}.constructor.prototype;
  1397. var ObjectKeys = function (o) {
  1398. var ret = [];
  1399. for (var key in o) {
  1400. if (has.call(o, key)) {
  1401. ret.push(key);
  1402. }
  1403. }
  1404. return ret;
  1405. };
  1406. var ObjectGetDescriptor = function(o, key) {
  1407. return {value: o[key]};
  1408. };
  1409. var ObjectDefineProperty = function (o, key, desc) {
  1410. o[key] = desc.value;
  1411. return o;
  1412. };
  1413. var ObjectFreeze = function (obj) {
  1414. return obj;
  1415. };
  1416. var ObjectGetPrototypeOf = function (obj) {
  1417. try {
  1418. return Object(obj).constructor.prototype;
  1419. }
  1420. catch (e) {
  1421. return proto;
  1422. }
  1423. };
  1424. var ArrayIsArray = function (obj) {
  1425. try {
  1426. return str.call(obj) === "[object Array]";
  1427. }
  1428. catch(e) {
  1429. return false;
  1430. }
  1431. };
  1432. module.exports = {
  1433. isArray: ArrayIsArray,
  1434. keys: ObjectKeys,
  1435. names: ObjectKeys,
  1436. defineProperty: ObjectDefineProperty,
  1437. getDescriptor: ObjectGetDescriptor,
  1438. freeze: ObjectFreeze,
  1439. getPrototypeOf: ObjectGetPrototypeOf,
  1440. isES5: isES5,
  1441. propertyIsWritable: function() {
  1442. return true;
  1443. }
  1444. };
  1445. }
  1446. },{}],11:[function(_dereq_,module,exports){
  1447. "use strict";
  1448. module.exports = function(Promise, tryConvertToPromise) {
  1449. var util = _dereq_("./util");
  1450. var CancellationError = Promise.CancellationError;
  1451. var errorObj = util.errorObj;
  1452. function PassThroughHandlerContext(promise, type, handler) {
  1453. this.promise = promise;
  1454. this.type = type;
  1455. this.handler = handler;
  1456. this.called = false;
  1457. this.cancelPromise = null;
  1458. }
  1459. PassThroughHandlerContext.prototype.isFinallyHandler = function() {
  1460. return this.type === 0;
  1461. };
  1462. function FinallyHandlerCancelReaction(finallyHandler) {
  1463. this.finallyHandler = finallyHandler;
  1464. }
  1465. FinallyHandlerCancelReaction.prototype._resultCancelled = function() {
  1466. checkCancel(this.finallyHandler);
  1467. };
  1468. function checkCancel(ctx, reason) {
  1469. if (ctx.cancelPromise != null) {
  1470. if (arguments.length > 1) {
  1471. ctx.cancelPromise._reject(reason);
  1472. } else {
  1473. ctx.cancelPromise._cancel();
  1474. }
  1475. ctx.cancelPromise = null;
  1476. return true;
  1477. }
  1478. return false;
  1479. }
  1480. function succeed() {
  1481. return finallyHandler.call(this, this.promise._target()._settledValue());
  1482. }
  1483. function fail(reason) {
  1484. if (checkCancel(this, reason)) return;
  1485. errorObj.e = reason;
  1486. return errorObj;
  1487. }
  1488. function finallyHandler(reasonOrValue) {
  1489. var promise = this.promise;
  1490. var handler = this.handler;
  1491. if (!this.called) {
  1492. this.called = true;
  1493. var ret = this.isFinallyHandler()
  1494. ? handler.call(promise._boundValue())
  1495. : handler.call(promise._boundValue(), reasonOrValue);
  1496. if (ret !== undefined) {
  1497. promise._setReturnedNonUndefined();
  1498. var maybePromise = tryConvertToPromise(ret, promise);
  1499. if (maybePromise instanceof Promise) {
  1500. if (this.cancelPromise != null) {
  1501. if (maybePromise.isCancelled()) {
  1502. var reason =
  1503. new CancellationError("late cancellation observer");
  1504. promise._attachExtraTrace(reason);
  1505. errorObj.e = reason;
  1506. return errorObj;
  1507. } else if (maybePromise.isPending()) {
  1508. maybePromise._attachCancellationCallback(
  1509. new FinallyHandlerCancelReaction(this));
  1510. }
  1511. }
  1512. return maybePromise._then(
  1513. succeed, fail, undefined, this, undefined);
  1514. }
  1515. }
  1516. }
  1517. if (promise.isRejected()) {
  1518. checkCancel(this);
  1519. errorObj.e = reasonOrValue;
  1520. return errorObj;
  1521. } else {
  1522. checkCancel(this);
  1523. return reasonOrValue;
  1524. }
  1525. }
  1526. Promise.prototype._passThrough = function(handler, type, success, fail) {
  1527. if (typeof handler !== "function") return this.then();
  1528. return this._then(success,
  1529. fail,
  1530. undefined,
  1531. new PassThroughHandlerContext(this, type, handler),
  1532. undefined);
  1533. };
  1534. Promise.prototype.lastly =
  1535. Promise.prototype["finally"] = function (handler) {
  1536. return this._passThrough(handler,
  1537. 0,
  1538. finallyHandler,
  1539. finallyHandler);
  1540. };
  1541. Promise.prototype.tap = function (handler) {
  1542. return this._passThrough(handler, 1, finallyHandler);
  1543. };
  1544. return PassThroughHandlerContext;
  1545. };
  1546. },{"./util":21}],12:[function(_dereq_,module,exports){
  1547. "use strict";
  1548. module.exports =
  1549. function(Promise, PromiseArray, tryConvertToPromise, INTERNAL) {
  1550. var util = _dereq_("./util");
  1551. var canEvaluate = util.canEvaluate;
  1552. var tryCatch = util.tryCatch;
  1553. var errorObj = util.errorObj;
  1554. var reject;
  1555. if (!true) {
  1556. if (canEvaluate) {
  1557. var thenCallback = function(i) {
  1558. return new Function("value", "holder", " \n\
  1559. 'use strict'; \n\
  1560. holder.pIndex = value; \n\
  1561. holder.checkFulfillment(this); \n\
  1562. ".replace(/Index/g, i));
  1563. };
  1564. var promiseSetter = function(i) {
  1565. return new Function("promise", "holder", " \n\
  1566. 'use strict'; \n\
  1567. holder.pIndex = promise; \n\
  1568. ".replace(/Index/g, i));
  1569. };
  1570. var generateHolderClass = function(total) {
  1571. var props = new Array(total);
  1572. for (var i = 0; i < props.length; ++i) {
  1573. props[i] = "this.p" + (i+1);
  1574. }
  1575. var assignment = props.join(" = ") + " = null;";
  1576. var cancellationCode= "var promise;\n" + props.map(function(prop) {
  1577. return " \n\
  1578. promise = " + prop + "; \n\
  1579. if (promise instanceof Promise) { \n\
  1580. promise.cancel(); \n\
  1581. } \n\
  1582. ";
  1583. }).join("\n");
  1584. var passedArguments = props.join(", ");
  1585. var name = "Holder$" + total;
  1586. var code = "return function(tryCatch, errorObj, Promise) { \n\
  1587. 'use strict'; \n\
  1588. function [TheName](fn) { \n\
  1589. [TheProperties] \n\
  1590. this.fn = fn; \n\
  1591. this.now = 0; \n\
  1592. } \n\
  1593. [TheName].prototype.checkFulfillment = function(promise) { \n\
  1594. var now = ++this.now; \n\
  1595. if (now === [TheTotal]) { \n\
  1596. promise._pushContext(); \n\
  1597. var callback = this.fn; \n\
  1598. var ret = tryCatch(callback)([ThePassedArguments]); \n\
  1599. promise._popContext(); \n\
  1600. if (ret === errorObj) { \n\
  1601. promise._rejectCallback(ret.e, false); \n\
  1602. } else { \n\
  1603. promise._resolveCallback(ret); \n\
  1604. } \n\
  1605. } \n\
  1606. }; \n\
  1607. \n\
  1608. [TheName].prototype._resultCancelled = function() { \n\
  1609. [CancellationCode] \n\
  1610. }; \n\
  1611. \n\
  1612. return [TheName]; \n\
  1613. }(tryCatch, errorObj, Promise); \n\
  1614. ";
  1615. code = code.replace(/\[TheName\]/g, name)
  1616. .replace(/\[TheTotal\]/g, total)
  1617. .replace(/\[ThePassedArguments\]/g, passedArguments)
  1618. .replace(/\[TheProperties\]/g, assignment)
  1619. .replace(/\[CancellationCode\]/g, cancellationCode);
  1620. return new Function("tryCatch", "errorObj", "Promise", code)
  1621. (tryCatch, errorObj, Promise);
  1622. };
  1623. var holderClasses = [];
  1624. var thenCallbacks = [];
  1625. var promiseSetters = [];
  1626. for (var i = 0; i < 8; ++i) {
  1627. holderClasses.push(generateHolderClass(i + 1));
  1628. thenCallbacks.push(thenCallback(i + 1));
  1629. promiseSetters.push(promiseSetter(i + 1));
  1630. }
  1631. reject = function (reason) {
  1632. this._reject(reason);
  1633. };
  1634. }}
  1635. Promise.join = function () {
  1636. var last = arguments.length - 1;
  1637. var fn;
  1638. if (last > 0 && typeof arguments[last] === "function") {
  1639. fn = arguments[last];
  1640. if (!true) {
  1641. if (last <= 8 && canEvaluate) {
  1642. var ret = new Promise(INTERNAL);
  1643. ret._captureStackTrace();
  1644. var HolderClass = holderClasses[last - 1];
  1645. var holder = new HolderClass(fn);
  1646. var callbacks = thenCallbacks;
  1647. for (var i = 0; i < last; ++i) {
  1648. var maybePromise = tryConvertToPromise(arguments[i], ret);
  1649. if (maybePromise instanceof Promise) {
  1650. maybePromise = maybePromise._target();
  1651. var bitField = maybePromise._bitField;
  1652. ;
  1653. if (((bitField & 50397184) === 0)) {
  1654. maybePromise._then(callbacks[i], reject,
  1655. undefined, ret, holder);
  1656. promiseSetters[i](maybePromise, holder);
  1657. } else if (((bitField & 33554432) !== 0)) {
  1658. callbacks[i].call(ret,
  1659. maybePromise._value(), holder);
  1660. } else if (((bitField & 16777216) !== 0)) {
  1661. ret._reject(maybePromise._reason());
  1662. } else {
  1663. ret._cancel();
  1664. }
  1665. } else {
  1666. callbacks[i].call(ret, maybePromise, holder);
  1667. }
  1668. }
  1669. if (!ret._isFateSealed()) {
  1670. ret._setAsyncGuaranteed();
  1671. ret._setOnCancel(holder);
  1672. }
  1673. return ret;
  1674. }
  1675. }
  1676. }
  1677. var args = [].slice.call(arguments);;
  1678. if (fn) args.pop();
  1679. var ret = new PromiseArray(args).promise();
  1680. return fn !== undefined ? ret.spread(fn) : ret;
  1681. };
  1682. };
  1683. },{"./util":21}],13:[function(_dereq_,module,exports){
  1684. "use strict";
  1685. module.exports =
  1686. function(Promise, INTERNAL, tryConvertToPromise, apiRejection, debug) {
  1687. var util = _dereq_("./util");
  1688. var tryCatch = util.tryCatch;
  1689. Promise.method = function (fn) {
  1690. if (typeof fn !== "function") {
  1691. throw new Promise.TypeError("expecting a function but got " + util.classString(fn));
  1692. }
  1693. return function () {
  1694. var ret = new Promise(INTERNAL);
  1695. ret._captureStackTrace();
  1696. ret._pushContext();
  1697. var value = tryCatch(fn).apply(this, arguments);
  1698. var promiseCreated = ret._popContext();
  1699. debug.checkForgottenReturns(
  1700. value, promiseCreated, "Promise.method", ret);
  1701. ret._resolveFromSyncValue(value);
  1702. return ret;
  1703. };
  1704. };
  1705. Promise.attempt = Promise["try"] = function (fn) {
  1706. if (typeof fn !== "function") {
  1707. return apiRejection("expecting a function but got " + util.classString(fn));
  1708. }
  1709. var ret = new Promise(INTERNAL);
  1710. ret._captureStackTrace();
  1711. ret._pushContext();
  1712. var value;
  1713. if (arguments.length > 1) {
  1714. debug.deprecated("calling Promise.try with more than 1 argument");
  1715. var arg = arguments[1];
  1716. var ctx = arguments[2];
  1717. value = util.isArray(arg) ? tryCatch(fn).apply(ctx, arg)
  1718. : tryCatch(fn).call(ctx, arg);
  1719. } else {
  1720. value = tryCatch(fn)();
  1721. }
  1722. var promiseCreated = ret._popContext();
  1723. debug.checkForgottenReturns(
  1724. value, promiseCreated, "Promise.try", ret);
  1725. ret._resolveFromSyncValue(value);
  1726. return ret;
  1727. };
  1728. Promise.prototype._resolveFromSyncValue = function (value) {
  1729. if (value === util.errorObj) {
  1730. this._rejectCallback(value.e, false);
  1731. } else {
  1732. this._resolveCallback(value, true);
  1733. }
  1734. };
  1735. };
  1736. },{"./util":21}],14:[function(_dereq_,module,exports){
  1737. "use strict";
  1738. var util = _dereq_("./util");
  1739. var maybeWrapAsError = util.maybeWrapAsError;
  1740. var errors = _dereq_("./errors");
  1741. var OperationalError = errors.OperationalError;
  1742. var es5 = _dereq_("./es5");
  1743. function isUntypedError(obj) {
  1744. return obj instanceof Error &&
  1745. es5.getPrototypeOf(obj) === Error.prototype;
  1746. }
  1747. var rErrorKey = /^(?:name|message|stack|cause)$/;
  1748. function wrapAsOperationalError(obj) {
  1749. var ret;
  1750. if (isUntypedError(obj)) {
  1751. ret = new OperationalError(obj);
  1752. ret.name = obj.name;
  1753. ret.message = obj.message;
  1754. ret.stack = obj.stack;
  1755. var keys = es5.keys(obj);
  1756. for (var i = 0; i < keys.length; ++i) {
  1757. var key = keys[i];
  1758. if (!rErrorKey.test(key)) {
  1759. ret[key] = obj[key];
  1760. }
  1761. }
  1762. return ret;
  1763. }
  1764. util.markAsOriginatingFromRejection(obj);
  1765. return obj;
  1766. }
  1767. function nodebackForPromise(promise, multiArgs) {
  1768. return function(err, value) {
  1769. if (promise === null) return;
  1770. if (err) {
  1771. var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
  1772. promise._attachExtraTrace(wrapped);
  1773. promise._reject(wrapped);
  1774. } else if (!multiArgs) {
  1775. promise._fulfill(value);
  1776. } else {
  1777. var args = [].slice.call(arguments, 1);;
  1778. promise._fulfill(args);
  1779. }
  1780. promise = null;
  1781. };
  1782. }
  1783. module.exports = nodebackForPromise;
  1784. },{"./errors":9,"./es5":10,"./util":21}],15:[function(_dereq_,module,exports){
  1785. "use strict";
  1786. module.exports = function() {
  1787. var makeSelfResolutionError = function () {
  1788. return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  1789. };
  1790. var reflectHandler = function() {
  1791. return new Promise.PromiseInspection(this._target());
  1792. };
  1793. var apiRejection = function(msg) {
  1794. return Promise.reject(new TypeError(msg));
  1795. };
  1796. function Proxyable() {}
  1797. var UNDEFINED_BINDING = {};
  1798. var util = _dereq_("./util");
  1799. var getDomain;
  1800. if (util.isNode) {
  1801. getDomain = function() {
  1802. var ret = process.domain;
  1803. if (ret === undefined) ret = null;
  1804. return ret;
  1805. };
  1806. } else {
  1807. getDomain = function() {
  1808. return null;
  1809. };
  1810. }
  1811. util.notEnumerableProp(Promise, "_getDomain", getDomain);
  1812. var es5 = _dereq_("./es5");
  1813. var Async = _dereq_("./async");
  1814. var async = new Async();
  1815. es5.defineProperty(Promise, "_async", {value: async});
  1816. var errors = _dereq_("./errors");
  1817. var TypeError = Promise.TypeError = errors.TypeError;
  1818. Promise.RangeError = errors.RangeError;
  1819. var CancellationError = Promise.CancellationError = errors.CancellationError;
  1820. Promise.TimeoutError = errors.TimeoutError;
  1821. Promise.OperationalError = errors.OperationalError;
  1822. Promise.RejectionError = errors.OperationalError;
  1823. Promise.AggregateError = errors.AggregateError;
  1824. var INTERNAL = function(){};
  1825. var APPLY = {};
  1826. var NEXT_FILTER = {};
  1827. var tryConvertToPromise = _dereq_("./thenables")(Promise, INTERNAL);
  1828. var PromiseArray =
  1829. _dereq_("./promise_array")(Promise, INTERNAL,
  1830. tryConvertToPromise, apiRejection, Proxyable);
  1831. var Context = _dereq_("./context")(Promise);
  1832. /*jshint unused:false*/
  1833. var createContext = Context.create;
  1834. var debug = _dereq_("./debuggability")(Promise, Context);
  1835. var CapturedTrace = debug.CapturedTrace;
  1836. var PassThroughHandlerContext =
  1837. _dereq_("./finally")(Promise, tryConvertToPromise);
  1838. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  1839. var nodebackForPromise = _dereq_("./nodeback");
  1840. var errorObj = util.errorObj;
  1841. var tryCatch = util.tryCatch;
  1842. function check(self, executor) {
  1843. if (typeof executor !== "function") {
  1844. throw new TypeError("expecting a function but got " + util.classString(executor));
  1845. }
  1846. if (self.constructor !== Promise) {
  1847. throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  1848. }
  1849. }
  1850. function Promise(executor) {
  1851. this._bitField = 0;
  1852. this._fulfillmentHandler0 = undefined;
  1853. this._rejectionHandler0 = undefined;
  1854. this._promise0 = undefined;
  1855. this._receiver0 = undefined;
  1856. if (executor !== INTERNAL) {
  1857. check(this, executor);
  1858. this._resolveFromExecutor(executor);
  1859. }
  1860. this._promiseCreated();
  1861. this._fireEvent("promiseCreated", this);
  1862. }
  1863. Promise.prototype.toString = function () {
  1864. return "[object Promise]";
  1865. };
  1866. Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
  1867. var len = arguments.length;
  1868. if (len > 1) {
  1869. var catchInstances = new Array(len - 1),
  1870. j = 0, i;
  1871. for (i = 0; i < len - 1; ++i) {
  1872. var item = arguments[i];
  1873. if (util.isObject(item)) {
  1874. catchInstances[j++] = item;
  1875. } else {
  1876. return apiRejection("expecting an object but got " + util.classString(item));
  1877. }
  1878. }
  1879. catchInstances.length = j;
  1880. fn = arguments[i];
  1881. return this.then(undefined, catchFilter(catchInstances, fn, this));
  1882. }
  1883. return this.then(undefined, fn);
  1884. };
  1885. Promise.prototype.reflect = function () {
  1886. return this._then(reflectHandler,
  1887. reflectHandler, undefined, this, undefined);
  1888. };
  1889. Promise.prototype.then = function (didFulfill, didReject) {
  1890. if (debug.warnings() && arguments.length > 0 &&
  1891. typeof didFulfill !== "function" &&
  1892. typeof didReject !== "function") {
  1893. var msg = ".then() only accepts functions but was passed: " +
  1894. util.classString(didFulfill);
  1895. if (arguments.length > 1) {
  1896. msg += ", " + util.classString(didReject);
  1897. }
  1898. this._warn(msg);
  1899. }
  1900. return this._then(didFulfill, didReject, undefined, undefined, undefined);
  1901. };
  1902. Promise.prototype.done = function (didFulfill, didReject) {
  1903. var promise =
  1904. this._then(didFulfill, didReject, undefined, undefined, undefined);
  1905. promise._setIsFinal();
  1906. };
  1907. Promise.prototype.spread = function (fn) {
  1908. if (typeof fn !== "function") {
  1909. return apiRejection("expecting a function but got " + util.classString(fn));
  1910. }
  1911. return this.all()._then(fn, undefined, undefined, APPLY, undefined);
  1912. };
  1913. Promise.prototype.toJSON = function () {
  1914. var ret = {
  1915. isFulfilled: false,
  1916. isRejected: false,
  1917. fulfillmentValue: undefined,
  1918. rejectionReason: undefined
  1919. };
  1920. if (this.isFulfilled()) {
  1921. ret.fulfillmentValue = this.value();
  1922. ret.isFulfilled = true;
  1923. } else if (this.isRejected()) {
  1924. ret.rejectionReason = this.reason();
  1925. ret.isRejected = true;
  1926. }
  1927. return ret;
  1928. };
  1929. Promise.prototype.all = function () {
  1930. if (arguments.length > 0) {
  1931. this._warn(".all() was passed arguments but it does not take any");
  1932. }
  1933. return new PromiseArray(this).promise();
  1934. };
  1935. Promise.prototype.error = function (fn) {
  1936. return this.caught(util.originatesFromRejection, fn);
  1937. };
  1938. Promise.is = function (val) {
  1939. return val instanceof Promise;
  1940. };
  1941. Promise.fromNode = Promise.fromCallback = function(fn) {
  1942. var ret = new Promise(INTERNAL);
  1943. ret._captureStackTrace();
  1944. var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
  1945. : false;
  1946. var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
  1947. if (result === errorObj) {
  1948. ret._rejectCallback(result.e, true);
  1949. }
  1950. if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
  1951. return ret;
  1952. };
  1953. Promise.all = function (promises) {
  1954. return new PromiseArray(promises).promise();
  1955. };
  1956. Promise.cast = function (obj) {
  1957. var ret = tryConvertToPromise(obj);
  1958. if (!(ret instanceof Promise)) {
  1959. ret = new Promise(INTERNAL);
  1960. ret._captureStackTrace();
  1961. ret._setFulfilled();
  1962. ret._rejectionHandler0 = obj;
  1963. }
  1964. return ret;
  1965. };
  1966. Promise.resolve = Promise.fulfilled = Promise.cast;
  1967. Promise.reject = Promise.rejected = function (reason) {
  1968. var ret = new Promise(INTERNAL);
  1969. ret._captureStackTrace();
  1970. ret._rejectCallback(reason, true);
  1971. return ret;
  1972. };
  1973. Promise.setScheduler = function(fn) {
  1974. if (typeof fn !== "function") {
  1975. throw new TypeError("expecting a function but got " + util.classString(fn));
  1976. }
  1977. var prev = async._schedule;
  1978. async._schedule = fn;
  1979. return prev;
  1980. };
  1981. Promise.prototype._then = function (
  1982. didFulfill,
  1983. didReject,
  1984. _, receiver,
  1985. internalData
  1986. ) {
  1987. var haveInternalData = internalData !== undefined;
  1988. var promise = haveInternalData ? internalData : new Promise(INTERNAL);
  1989. var target = this._target();
  1990. var bitField = target._bitField;
  1991. if (!haveInternalData) {
  1992. promise._propagateFrom(this, 3);
  1993. promise._captureStackTrace();
  1994. if (receiver === undefined &&
  1995. ((this._bitField & 2097152) !== 0)) {
  1996. if (!((bitField & 50397184) === 0)) {
  1997. receiver = this._boundValue();
  1998. } else {
  1999. receiver = target === this ? undefined : this._boundTo;
  2000. }
  2001. }
  2002. this._fireEvent("promiseChained", this, promise);
  2003. }
  2004. var domain = getDomain();
  2005. if (!((bitField & 50397184) === 0)) {
  2006. var handler, value, settler = target._settlePromiseCtx;
  2007. if (((bitField & 33554432) !== 0)) {
  2008. value = target._rejectionHandler0;
  2009. handler = didFulfill;
  2010. } else if (((bitField & 16777216) !== 0)) {
  2011. value = target._fulfillmentHandler0;
  2012. handler = didReject;
  2013. target._unsetRejectionIsUnhandled();
  2014. } else {
  2015. settler = target._settlePromiseLateCancellationObserver;
  2016. value = new CancellationError("late cancellation observer");
  2017. target._attachExtraTrace(value);
  2018. handler = didReject;
  2019. }
  2020. async.invoke(settler, target, {
  2021. handler: domain === null ? handler
  2022. : (typeof handler === "function" && domain.bind(handler)),
  2023. promise: promise,
  2024. receiver: receiver,
  2025. value: value
  2026. });
  2027. } else {
  2028. target._addCallbacks(didFulfill, didReject, promise, receiver, domain);
  2029. }
  2030. return promise;
  2031. };
  2032. Promise.prototype._length = function () {
  2033. return this._bitField & 65535;
  2034. };
  2035. Promise.prototype._isFateSealed = function () {
  2036. return (this._bitField & 117506048) !== 0;
  2037. };
  2038. Promise.prototype._isFollowing = function () {
  2039. return (this._bitField & 67108864) === 67108864;
  2040. };
  2041. Promise.prototype._setLength = function (len) {
  2042. this._bitField = (this._bitField & -65536) |
  2043. (len & 65535);
  2044. };
  2045. Promise.prototype._setFulfilled = function () {
  2046. this._bitField = this._bitField | 33554432;
  2047. this._fireEvent("promiseFulfilled", this);
  2048. };
  2049. Promise.prototype._setRejected = function () {
  2050. this._bitField = this._bitField | 16777216;
  2051. this._fireEvent("promiseRejected", this);
  2052. };
  2053. Promise.prototype._setFollowing = function () {
  2054. this._bitField = this._bitField | 67108864;
  2055. this._fireEvent("promiseResolved", this);
  2056. };
  2057. Promise.prototype._setIsFinal = function () {
  2058. this._bitField = this._bitField | 4194304;
  2059. };
  2060. Promise.prototype._isFinal = function () {
  2061. return (this._bitField & 4194304) > 0;
  2062. };
  2063. Promise.prototype._unsetCancelled = function() {
  2064. this._bitField = this._bitField & (~65536);
  2065. };
  2066. Promise.prototype._setCancelled = function() {
  2067. this._bitField = this._bitField | 65536;
  2068. this._fireEvent("promiseCancelled", this);
  2069. };
  2070. Promise.prototype._setAsyncGuaranteed = function() {
  2071. this._bitField = this._bitField | 134217728;
  2072. };
  2073. Promise.prototype._receiverAt = function (index) {
  2074. var ret = index === 0 ? this._receiver0 : this[
  2075. index * 4 - 4 + 3];
  2076. if (ret === UNDEFINED_BINDING) {
  2077. return undefined;
  2078. } else if (ret === undefined && this._isBound()) {
  2079. return this._boundValue();
  2080. }
  2081. return ret;
  2082. };
  2083. Promise.prototype._promiseAt = function (index) {
  2084. return this[
  2085. index * 4 - 4 + 2];
  2086. };
  2087. Promise.prototype._fulfillmentHandlerAt = function (index) {
  2088. return this[
  2089. index * 4 - 4 + 0];
  2090. };
  2091. Promise.prototype._rejectionHandlerAt = function (index) {
  2092. return this[
  2093. index * 4 - 4 + 1];
  2094. };
  2095. Promise.prototype._boundValue = function() {};
  2096. Promise.prototype._migrateCallback0 = function (follower) {
  2097. var bitField = follower._bitField;
  2098. var fulfill = follower._fulfillmentHandler0;
  2099. var reject = follower._rejectionHandler0;
  2100. var promise = follower._promise0;
  2101. var receiver = follower._receiverAt(0);
  2102. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2103. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2104. };
  2105. Promise.prototype._migrateCallbackAt = function (follower, index) {
  2106. var fulfill = follower._fulfillmentHandlerAt(index);
  2107. var reject = follower._rejectionHandlerAt(index);
  2108. var promise = follower._promiseAt(index);
  2109. var receiver = follower._receiverAt(index);
  2110. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2111. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2112. };
  2113. Promise.prototype._addCallbacks = function (
  2114. fulfill,
  2115. reject,
  2116. promise,
  2117. receiver,
  2118. domain
  2119. ) {
  2120. var index = this._length();
  2121. if (index >= 65535 - 4) {
  2122. index = 0;
  2123. this._setLength(0);
  2124. }
  2125. if (index === 0) {
  2126. this._promise0 = promise;
  2127. this._receiver0 = receiver;
  2128. if (typeof fulfill === "function") {
  2129. this._fulfillmentHandler0 =
  2130. domain === null ? fulfill : domain.bind(fulfill);
  2131. }
  2132. if (typeof reject === "function") {
  2133. this._rejectionHandler0 =
  2134. domain === null ? reject : domain.bind(reject);
  2135. }
  2136. } else {
  2137. var base = index * 4 - 4;
  2138. this[base + 2] = promise;
  2139. this[base + 3] = receiver;
  2140. if (typeof fulfill === "function") {
  2141. this[base + 0] =
  2142. domain === null ? fulfill : domain.bind(fulfill);
  2143. }
  2144. if (typeof reject === "function") {
  2145. this[base + 1] =
  2146. domain === null ? reject : domain.bind(reject);
  2147. }
  2148. }
  2149. this._setLength(index + 1);
  2150. return index;
  2151. };
  2152. Promise.prototype._proxy = function (proxyable, arg) {
  2153. this._addCallbacks(undefined, undefined, arg, proxyable, null);
  2154. };
  2155. Promise.prototype._resolveCallback = function(value, shouldBind) {
  2156. if (((this._bitField & 117506048) !== 0)) return;
  2157. if (value === this)
  2158. return this._rejectCallback(makeSelfResolutionError(), false);
  2159. var maybePromise = tryConvertToPromise(value, this);
  2160. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  2161. if (shouldBind) this._propagateFrom(maybePromise, 2);
  2162. var promise = maybePromise._target();
  2163. if (promise === this) {
  2164. this._reject(makeSelfResolutionError());
  2165. return;
  2166. }
  2167. var bitField = promise._bitField;
  2168. if (((bitField & 50397184) === 0)) {
  2169. var len = this._length();
  2170. if (len > 0) promise._migrateCallback0(this);
  2171. for (var i = 1; i < len; ++i) {
  2172. promise._migrateCallbackAt(this, i);
  2173. }
  2174. this._setFollowing();
  2175. this._setLength(0);
  2176. this._setFollowee(promise);
  2177. } else if (((bitField & 33554432) !== 0)) {
  2178. this._fulfill(promise._value());
  2179. } else if (((bitField & 16777216) !== 0)) {
  2180. this._reject(promise._reason());
  2181. } else {
  2182. var reason = new CancellationError("late cancellation observer");
  2183. promise._attachExtraTrace(reason);
  2184. this._reject(reason);
  2185. }
  2186. };
  2187. Promise.prototype._rejectCallback =
  2188. function(reason, synchronous, ignoreNonErrorWarnings) {
  2189. var trace = util.ensureErrorObject(reason);
  2190. var hasStack = trace === reason;
  2191. if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
  2192. var message = "a promise was rejected with a non-error: " +
  2193. util.classString(reason);
  2194. this._warn(message, true);
  2195. }
  2196. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  2197. this._reject(reason);
  2198. };
  2199. Promise.prototype._resolveFromExecutor = function (executor) {
  2200. var promise = this;
  2201. this._captureStackTrace();
  2202. this._pushContext();
  2203. var synchronous = true;
  2204. var r = this._execute(executor, function(value) {
  2205. promise._resolveCallback(value);
  2206. }, function (reason) {
  2207. promise._rejectCallback(reason, synchronous);
  2208. });
  2209. synchronous = false;
  2210. this._popContext();
  2211. if (r !== undefined) {
  2212. promise._rejectCallback(r, true);
  2213. }
  2214. };
  2215. Promise.prototype._settlePromiseFromHandler = function (
  2216. handler, receiver, value, promise
  2217. ) {
  2218. var bitField = promise._bitField;
  2219. if (((bitField & 65536) !== 0)) return;
  2220. promise._pushContext();
  2221. var x;
  2222. if (receiver === APPLY) {
  2223. if (!value || typeof value.length !== "number") {
  2224. x = errorObj;
  2225. x.e = new TypeError("cannot .spread() a non-array: " +
  2226. util.classString(value));
  2227. } else {
  2228. x = tryCatch(handler).apply(this._boundValue(), value);
  2229. }
  2230. } else {
  2231. x = tryCatch(handler).call(receiver, value);
  2232. }
  2233. var promiseCreated = promise._popContext();
  2234. bitField = promise._bitField;
  2235. if (((bitField & 65536) !== 0)) return;
  2236. if (x === NEXT_FILTER) {
  2237. promise._reject(value);
  2238. } else if (x === errorObj) {
  2239. promise._rejectCallback(x.e, false);
  2240. } else {
  2241. debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
  2242. promise._resolveCallback(x);
  2243. }
  2244. };
  2245. Promise.prototype._target = function() {
  2246. var ret = this;
  2247. while (ret._isFollowing()) ret = ret._followee();
  2248. return ret;
  2249. };
  2250. Promise.prototype._followee = function() {
  2251. return this._rejectionHandler0;
  2252. };
  2253. Promise.prototype._setFollowee = function(promise) {
  2254. this._rejectionHandler0 = promise;
  2255. };
  2256. Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
  2257. var isPromise = promise instanceof Promise;
  2258. var bitField = this._bitField;
  2259. var asyncGuaranteed = ((bitField & 134217728) !== 0);
  2260. if (((bitField & 65536) !== 0)) {
  2261. if (isPromise) promise._invokeInternalOnCancel();
  2262. if (receiver instanceof PassThroughHandlerContext &&
  2263. receiver.isFinallyHandler()) {
  2264. receiver.cancelPromise = promise;
  2265. if (tryCatch(handler).call(receiver, value) === errorObj) {
  2266. promise._reject(errorObj.e);
  2267. }
  2268. } else if (handler === reflectHandler) {
  2269. promise._fulfill(reflectHandler.call(receiver));
  2270. } else if (receiver instanceof Proxyable) {
  2271. receiver._promiseCancelled(promise);
  2272. } else if (isPromise || promise instanceof PromiseArray) {
  2273. promise._cancel();
  2274. } else {
  2275. receiver.cancel();
  2276. }
  2277. } else if (typeof handler === "function") {
  2278. if (!isPromise) {
  2279. handler.call(receiver, value, promise);
  2280. } else {
  2281. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2282. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2283. }
  2284. } else if (receiver instanceof Proxyable) {
  2285. if (!receiver._isResolved()) {
  2286. if (((bitField & 33554432) !== 0)) {
  2287. receiver._promiseFulfilled(value, promise);
  2288. } else {
  2289. receiver._promiseRejected(value, promise);
  2290. }
  2291. }
  2292. } else if (isPromise) {
  2293. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2294. if (((bitField & 33554432) !== 0)) {
  2295. promise._fulfill(value);
  2296. } else {
  2297. promise._reject(value);
  2298. }
  2299. }
  2300. };
  2301. Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
  2302. var handler = ctx.handler;
  2303. var promise = ctx.promise;
  2304. var receiver = ctx.receiver;
  2305. var value = ctx.value;
  2306. if (typeof handler === "function") {
  2307. if (!(promise instanceof Promise)) {
  2308. handler.call(receiver, value, promise);
  2309. } else {
  2310. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2311. }
  2312. } else if (promise instanceof Promise) {
  2313. promise._reject(value);
  2314. }
  2315. };
  2316. Promise.prototype._settlePromiseCtx = function(ctx) {
  2317. this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
  2318. };
  2319. Promise.prototype._settlePromise0 = function(handler, value, bitField) {
  2320. var promise = this._promise0;
  2321. var receiver = this._receiverAt(0);
  2322. this._promise0 = undefined;
  2323. this._receiver0 = undefined;
  2324. this._settlePromise(promise, handler, receiver, value);
  2325. };
  2326. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  2327. var base = index * 4 - 4;
  2328. this[base + 2] =
  2329. this[base + 3] =
  2330. this[base + 0] =
  2331. this[base + 1] = undefined;
  2332. };
  2333. Promise.prototype._fulfill = function (value) {
  2334. var bitField = this._bitField;
  2335. if (((bitField & 117506048) >>> 16)) return;
  2336. if (value === this) {
  2337. var err = makeSelfResolutionError();
  2338. this._attachExtraTrace(err);
  2339. return this._reject(err);
  2340. }
  2341. this._setFulfilled();
  2342. this._rejectionHandler0 = value;
  2343. if ((bitField & 65535) > 0) {
  2344. if (((bitField & 134217728) !== 0)) {
  2345. this._settlePromises();
  2346. } else {
  2347. async.settlePromises(this);
  2348. }
  2349. }
  2350. };
  2351. Promise.prototype._reject = function (reason) {
  2352. var bitField = this._bitField;
  2353. if (((bitField & 117506048) >>> 16)) return;
  2354. this._setRejected();
  2355. this._fulfillmentHandler0 = reason;
  2356. if (this._isFinal()) {
  2357. return async.fatalError(reason, util.isNode);
  2358. }
  2359. if ((bitField & 65535) > 0) {
  2360. async.settlePromises(this);
  2361. } else {
  2362. this._ensurePossibleRejectionHandled();
  2363. }
  2364. };
  2365. Promise.prototype._fulfillPromises = function (len, value) {
  2366. for (var i = 1; i < len; i++) {
  2367. var handler = this._fulfillmentHandlerAt(i);
  2368. var promise = this._promiseAt(i);
  2369. var receiver = this._receiverAt(i);
  2370. this._clearCallbackDataAtIndex(i);
  2371. this._settlePromise(promise, handler, receiver, value);
  2372. }
  2373. };
  2374. Promise.prototype._rejectPromises = function (len, reason) {
  2375. for (var i = 1; i < len; i++) {
  2376. var handler = this._rejectionHandlerAt(i);
  2377. var promise = this._promiseAt(i);
  2378. var receiver = this._receiverAt(i);
  2379. this._clearCallbackDataAtIndex(i);
  2380. this._settlePromise(promise, handler, receiver, reason);
  2381. }
  2382. };
  2383. Promise.prototype._settlePromises = function () {
  2384. var bitField = this._bitField;
  2385. var len = (bitField & 65535);
  2386. if (len > 0) {
  2387. if (((bitField & 16842752) !== 0)) {
  2388. var reason = this._fulfillmentHandler0;
  2389. this._settlePromise0(this._rejectionHandler0, reason, bitField);
  2390. this._rejectPromises(len, reason);
  2391. } else {
  2392. var value = this._rejectionHandler0;
  2393. this._settlePromise0(this._fulfillmentHandler0, value, bitField);
  2394. this._fulfillPromises(len, value);
  2395. }
  2396. this._setLength(0);
  2397. }
  2398. this._clearCancellationData();
  2399. };
  2400. Promise.prototype._settledValue = function() {
  2401. var bitField = this._bitField;
  2402. if (((bitField & 33554432) !== 0)) {
  2403. return this._rejectionHandler0;
  2404. } else if (((bitField & 16777216) !== 0)) {
  2405. return this._fulfillmentHandler0;
  2406. }
  2407. };
  2408. function deferResolve(v) {this.promise._resolveCallback(v);}
  2409. function deferReject(v) {this.promise._rejectCallback(v, false);}
  2410. Promise.defer = Promise.pending = function() {
  2411. debug.deprecated("Promise.defer", "new Promise");
  2412. var promise = new Promise(INTERNAL);
  2413. return {
  2414. promise: promise,
  2415. resolve: deferResolve,
  2416. reject: deferReject
  2417. };
  2418. };
  2419. util.notEnumerableProp(Promise,
  2420. "_makeSelfResolutionError",
  2421. makeSelfResolutionError);
  2422. _dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
  2423. debug);
  2424. _dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
  2425. _dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug);
  2426. _dereq_("./direct_resolve")(Promise);
  2427. _dereq_("./synchronous_inspection")(Promise);
  2428. _dereq_("./join")(
  2429. Promise, PromiseArray, tryConvertToPromise, INTERNAL, debug);
  2430. Promise.Promise = Promise;
  2431. util.toFastProperties(Promise);
  2432. util.toFastProperties(Promise.prototype);
  2433. function fillTypes(value) {
  2434. var p = new Promise(INTERNAL);
  2435. p._fulfillmentHandler0 = value;
  2436. p._rejectionHandler0 = value;
  2437. p._promise0 = value;
  2438. p._receiver0 = value;
  2439. }
  2440. // Complete slack tracking, opt out of field-type tracking and
  2441. // stabilize map
  2442. fillTypes({a: 1});
  2443. fillTypes({b: 2});
  2444. fillTypes({c: 3});
  2445. fillTypes(1);
  2446. fillTypes(function(){});
  2447. fillTypes(undefined);
  2448. fillTypes(false);
  2449. fillTypes(new Promise(INTERNAL));
  2450. debug.setBounds(Async.firstLineError, util.lastLineError);
  2451. return Promise;
  2452. };
  2453. },{"./async":1,"./bind":2,"./cancel":4,"./catch_filter":5,"./context":6,"./debuggability":7,"./direct_resolve":8,"./errors":9,"./es5":10,"./finally":11,"./join":12,"./method":13,"./nodeback":14,"./promise_array":16,"./synchronous_inspection":19,"./thenables":20,"./util":21}],16:[function(_dereq_,module,exports){
  2454. "use strict";
  2455. module.exports = function(Promise, INTERNAL, tryConvertToPromise,
  2456. apiRejection, Proxyable) {
  2457. var util = _dereq_("./util");
  2458. var isArray = util.isArray;
  2459. function toResolutionValue(val) {
  2460. switch(val) {
  2461. case -2: return [];
  2462. case -3: return {};
  2463. }
  2464. }
  2465. function PromiseArray(values) {
  2466. var promise = this._promise = new Promise(INTERNAL);
  2467. if (values instanceof Promise) {
  2468. promise._propagateFrom(values, 3);
  2469. }
  2470. promise._setOnCancel(this);
  2471. this._values = values;
  2472. this._length = 0;
  2473. this._totalResolved = 0;
  2474. this._init(undefined, -2);
  2475. }
  2476. util.inherits(PromiseArray, Proxyable);
  2477. PromiseArray.prototype.length = function () {
  2478. return this._length;
  2479. };
  2480. PromiseArray.prototype.promise = function () {
  2481. return this._promise;
  2482. };
  2483. PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
  2484. var values = tryConvertToPromise(this._values, this._promise);
  2485. if (values instanceof Promise) {
  2486. values = values._target();
  2487. var bitField = values._bitField;
  2488. ;
  2489. this._values = values;
  2490. if (((bitField & 50397184) === 0)) {
  2491. this._promise._setAsyncGuaranteed();
  2492. return values._then(
  2493. init,
  2494. this._reject,
  2495. undefined,
  2496. this,
  2497. resolveValueIfEmpty
  2498. );
  2499. } else if (((bitField & 33554432) !== 0)) {
  2500. values = values._value();
  2501. } else if (((bitField & 16777216) !== 0)) {
  2502. return this._reject(values._reason());
  2503. } else {
  2504. return this._cancel();
  2505. }
  2506. }
  2507. values = util.asArray(values);
  2508. if (values === null) {
  2509. var err = apiRejection(
  2510. "expecting an array or an iterable object but got " + util.classString(values)).reason();
  2511. this._promise._rejectCallback(err, false);
  2512. return;
  2513. }
  2514. if (values.length === 0) {
  2515. if (resolveValueIfEmpty === -5) {
  2516. this._resolveEmptyArray();
  2517. }
  2518. else {
  2519. this._resolve(toResolutionValue(resolveValueIfEmpty));
  2520. }
  2521. return;
  2522. }
  2523. this._iterate(values);
  2524. };
  2525. PromiseArray.prototype._iterate = function(values) {
  2526. var len = this.getActualLength(values.length);
  2527. this._length = len;
  2528. this._values = this.shouldCopyValues() ? new Array(len) : this._values;
  2529. var result = this._promise;
  2530. var isResolved = false;
  2531. var bitField = null;
  2532. for (var i = 0; i < len; ++i) {
  2533. var maybePromise = tryConvertToPromise(values[i], result);
  2534. if (maybePromise instanceof Promise) {
  2535. maybePromise = maybePromise._target();
  2536. bitField = maybePromise._bitField;
  2537. } else {
  2538. bitField = null;
  2539. }
  2540. if (isResolved) {
  2541. if (bitField !== null) {
  2542. maybePromise.suppressUnhandledRejections();
  2543. }
  2544. } else if (bitField !== null) {
  2545. if (((bitField & 50397184) === 0)) {
  2546. maybePromise._proxy(this, i);
  2547. this._values[i] = maybePromise;
  2548. } else if (((bitField & 33554432) !== 0)) {
  2549. isResolved = this._promiseFulfilled(maybePromise._value(), i);
  2550. } else if (((bitField & 16777216) !== 0)) {
  2551. isResolved = this._promiseRejected(maybePromise._reason(), i);
  2552. } else {
  2553. isResolved = this._promiseCancelled(i);
  2554. }
  2555. } else {
  2556. isResolved = this._promiseFulfilled(maybePromise, i);
  2557. }
  2558. }
  2559. if (!isResolved) result._setAsyncGuaranteed();
  2560. };
  2561. PromiseArray.prototype._isResolved = function () {
  2562. return this._values === null;
  2563. };
  2564. PromiseArray.prototype._resolve = function (value) {
  2565. this._values = null;
  2566. this._promise._fulfill(value);
  2567. };
  2568. PromiseArray.prototype._cancel = function() {
  2569. if (this._isResolved() || !this._promise.isCancellable()) return;
  2570. this._values = null;
  2571. this._promise._cancel();
  2572. };
  2573. PromiseArray.prototype._reject = function (reason) {
  2574. this._values = null;
  2575. this._promise._rejectCallback(reason, false);
  2576. };
  2577. PromiseArray.prototype._promiseFulfilled = function (value, index) {
  2578. this._values[index] = value;
  2579. var totalResolved = ++this._totalResolved;
  2580. if (totalResolved >= this._length) {
  2581. this._resolve(this._values);
  2582. return true;
  2583. }
  2584. return false;
  2585. };
  2586. PromiseArray.prototype._promiseCancelled = function() {
  2587. this._cancel();
  2588. return true;
  2589. };
  2590. PromiseArray.prototype._promiseRejected = function (reason) {
  2591. this._totalResolved++;
  2592. this._reject(reason);
  2593. return true;
  2594. };
  2595. PromiseArray.prototype._resultCancelled = function() {
  2596. if (this._isResolved()) return;
  2597. var values = this._values;
  2598. this._cancel();
  2599. if (values instanceof Promise) {
  2600. values.cancel();
  2601. } else {
  2602. for (var i = 0; i < values.length; ++i) {
  2603. if (values[i] instanceof Promise) {
  2604. values[i].cancel();
  2605. }
  2606. }
  2607. }
  2608. };
  2609. PromiseArray.prototype.shouldCopyValues = function () {
  2610. return true;
  2611. };
  2612. PromiseArray.prototype.getActualLength = function (len) {
  2613. return len;
  2614. };
  2615. return PromiseArray;
  2616. };
  2617. },{"./util":21}],17:[function(_dereq_,module,exports){
  2618. "use strict";
  2619. function arrayMove(src, srcIndex, dst, dstIndex, len) {
  2620. for (var j = 0; j < len; ++j) {
  2621. dst[j + dstIndex] = src[j + srcIndex];
  2622. src[j + srcIndex] = void 0;
  2623. }
  2624. }
  2625. function Queue(capacity) {
  2626. this._capacity = capacity;
  2627. this._length = 0;
  2628. this._front = 0;
  2629. }
  2630. Queue.prototype._willBeOverCapacity = function (size) {
  2631. return this._capacity < size;
  2632. };
  2633. Queue.prototype._pushOne = function (arg) {
  2634. var length = this.length();
  2635. this._checkCapacity(length + 1);
  2636. var i = (this._front + length) & (this._capacity - 1);
  2637. this[i] = arg;
  2638. this._length = length + 1;
  2639. };
  2640. Queue.prototype._unshiftOne = function(value) {
  2641. var capacity = this._capacity;
  2642. this._checkCapacity(this.length() + 1);
  2643. var front = this._front;
  2644. var i = (((( front - 1 ) &
  2645. ( capacity - 1) ) ^ capacity ) - capacity );
  2646. this[i] = value;
  2647. this._front = i;
  2648. this._length = this.length() + 1;
  2649. };
  2650. Queue.prototype.unshift = function(fn, receiver, arg) {
  2651. this._unshiftOne(arg);
  2652. this._unshiftOne(receiver);
  2653. this._unshiftOne(fn);
  2654. };
  2655. Queue.prototype.push = function (fn, receiver, arg) {
  2656. var length = this.length() + 3;
  2657. if (this._willBeOverCapacity(length)) {
  2658. this._pushOne(fn);
  2659. this._pushOne(receiver);
  2660. this._pushOne(arg);
  2661. return;
  2662. }
  2663. var j = this._front + length - 3;
  2664. this._checkCapacity(length);
  2665. var wrapMask = this._capacity - 1;
  2666. this[(j + 0) & wrapMask] = fn;
  2667. this[(j + 1) & wrapMask] = receiver;
  2668. this[(j + 2) & wrapMask] = arg;
  2669. this._length = length;
  2670. };
  2671. Queue.prototype.shift = function () {
  2672. var front = this._front,
  2673. ret = this[front];
  2674. this[front] = undefined;
  2675. this._front = (front + 1) & (this._capacity - 1);
  2676. this._length--;
  2677. return ret;
  2678. };
  2679. Queue.prototype.length = function () {
  2680. return this._length;
  2681. };
  2682. Queue.prototype._checkCapacity = function (size) {
  2683. if (this._capacity < size) {
  2684. this._resizeTo(this._capacity << 1);
  2685. }
  2686. };
  2687. Queue.prototype._resizeTo = function (capacity) {
  2688. var oldCapacity = this._capacity;
  2689. this._capacity = capacity;
  2690. var front = this._front;
  2691. var length = this._length;
  2692. var moveItemsCount = (front + length) & (oldCapacity - 1);
  2693. arrayMove(this, 0, this, oldCapacity, moveItemsCount);
  2694. };
  2695. module.exports = Queue;
  2696. },{}],18:[function(_dereq_,module,exports){
  2697. "use strict";
  2698. var util = _dereq_("./util");
  2699. var schedule;
  2700. var noAsyncScheduler = function() {
  2701. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2702. };
  2703. if (util.isNode && typeof MutationObserver === "undefined") {
  2704. var GlobalSetImmediate = global.setImmediate;
  2705. var ProcessNextTick = process.nextTick;
  2706. schedule = util.isRecentNode
  2707. ? function(fn) { GlobalSetImmediate.call(global, fn); }
  2708. : function(fn) { ProcessNextTick.call(process, fn); };
  2709. } else if ((typeof MutationObserver !== "undefined") &&
  2710. !(typeof window !== "undefined" &&
  2711. window.navigator &&
  2712. window.navigator.standalone)) {
  2713. schedule = (function() {
  2714. var div = document.createElement("div");
  2715. var opts = {attributes: true};
  2716. var toggleScheduled = false;
  2717. var div2 = document.createElement("div");
  2718. var o2 = new MutationObserver(function() {
  2719. div.classList.toggle("foo");
  2720. toggleScheduled = false;
  2721. });
  2722. o2.observe(div2, opts);
  2723. var scheduleToggle = function() {
  2724. if (toggleScheduled) return;
  2725. toggleScheduled = true;
  2726. div2.classList.toggle("foo");
  2727. };
  2728. return function schedule(fn) {
  2729. var o = new MutationObserver(function() {
  2730. o.disconnect();
  2731. fn();
  2732. });
  2733. o.observe(div, opts);
  2734. scheduleToggle();
  2735. };
  2736. })();
  2737. } else if (typeof setImmediate !== "undefined") {
  2738. schedule = function (fn) {
  2739. setImmediate(fn);
  2740. };
  2741. } else if (typeof setTimeout !== "undefined") {
  2742. schedule = function (fn) {
  2743. setTimeout(fn, 0);
  2744. };
  2745. } else {
  2746. schedule = noAsyncScheduler;
  2747. }
  2748. module.exports = schedule;
  2749. },{"./util":21}],19:[function(_dereq_,module,exports){
  2750. "use strict";
  2751. module.exports = function(Promise) {
  2752. function PromiseInspection(promise) {
  2753. if (promise !== undefined) {
  2754. promise = promise._target();
  2755. this._bitField = promise._bitField;
  2756. this._settledValueField = promise._isFateSealed()
  2757. ? promise._settledValue() : undefined;
  2758. }
  2759. else {
  2760. this._bitField = 0;
  2761. this._settledValueField = undefined;
  2762. }
  2763. }
  2764. PromiseInspection.prototype._settledValue = function() {
  2765. return this._settledValueField;
  2766. };
  2767. var value = PromiseInspection.prototype.value = function () {
  2768. if (!this.isFulfilled()) {
  2769. throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2770. }
  2771. return this._settledValue();
  2772. };
  2773. var reason = PromiseInspection.prototype.error =
  2774. PromiseInspection.prototype.reason = function () {
  2775. if (!this.isRejected()) {
  2776. throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2777. }
  2778. return this._settledValue();
  2779. };
  2780. var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
  2781. return (this._bitField & 33554432) !== 0;
  2782. };
  2783. var isRejected = PromiseInspection.prototype.isRejected = function () {
  2784. return (this._bitField & 16777216) !== 0;
  2785. };
  2786. var isPending = PromiseInspection.prototype.isPending = function () {
  2787. return (this._bitField & 50397184) === 0;
  2788. };
  2789. var isResolved = PromiseInspection.prototype.isResolved = function () {
  2790. return (this._bitField & 50331648) !== 0;
  2791. };
  2792. PromiseInspection.prototype.isCancelled =
  2793. Promise.prototype._isCancelled = function() {
  2794. return (this._bitField & 65536) === 65536;
  2795. };
  2796. Promise.prototype.isCancelled = function() {
  2797. return this._target()._isCancelled();
  2798. };
  2799. Promise.prototype.isPending = function() {
  2800. return isPending.call(this._target());
  2801. };
  2802. Promise.prototype.isRejected = function() {
  2803. return isRejected.call(this._target());
  2804. };
  2805. Promise.prototype.isFulfilled = function() {
  2806. return isFulfilled.call(this._target());
  2807. };
  2808. Promise.prototype.isResolved = function() {
  2809. return isResolved.call(this._target());
  2810. };
  2811. Promise.prototype.value = function() {
  2812. return value.call(this._target());
  2813. };
  2814. Promise.prototype.reason = function() {
  2815. var target = this._target();
  2816. target._unsetRejectionIsUnhandled();
  2817. return reason.call(target);
  2818. };
  2819. Promise.prototype._value = function() {
  2820. return this._settledValue();
  2821. };
  2822. Promise.prototype._reason = function() {
  2823. this._unsetRejectionIsUnhandled();
  2824. return this._settledValue();
  2825. };
  2826. Promise.PromiseInspection = PromiseInspection;
  2827. };
  2828. },{}],20:[function(_dereq_,module,exports){
  2829. "use strict";
  2830. module.exports = function(Promise, INTERNAL) {
  2831. var util = _dereq_("./util");
  2832. var errorObj = util.errorObj;
  2833. var isObject = util.isObject;
  2834. function tryConvertToPromise(obj, context) {
  2835. if (isObject(obj)) {
  2836. if (obj instanceof Promise) return obj;
  2837. var then = getThen(obj);
  2838. if (then === errorObj) {
  2839. if (context) context._pushContext();
  2840. var ret = Promise.reject(then.e);
  2841. if (context) context._popContext();
  2842. return ret;
  2843. } else if (typeof then === "function") {
  2844. if (isAnyBluebirdPromise(obj)) {
  2845. var ret = new Promise(INTERNAL);
  2846. obj._then(
  2847. ret._fulfill,
  2848. ret._reject,
  2849. undefined,
  2850. ret,
  2851. null
  2852. );
  2853. return ret;
  2854. }
  2855. return doThenable(obj, then, context);
  2856. }
  2857. }
  2858. return obj;
  2859. }
  2860. function doGetThen(obj) {
  2861. return obj.then;
  2862. }
  2863. function getThen(obj) {
  2864. try {
  2865. return doGetThen(obj);
  2866. } catch (e) {
  2867. errorObj.e = e;
  2868. return errorObj;
  2869. }
  2870. }
  2871. var hasProp = {}.hasOwnProperty;
  2872. function isAnyBluebirdPromise(obj) {
  2873. return hasProp.call(obj, "_promise0");
  2874. }
  2875. function doThenable(x, then, context) {
  2876. var promise = new Promise(INTERNAL);
  2877. var ret = promise;
  2878. if (context) context._pushContext();
  2879. promise._captureStackTrace();
  2880. if (context) context._popContext();
  2881. var synchronous = true;
  2882. var result = util.tryCatch(then).call(x, resolve, reject);
  2883. synchronous = false;
  2884. if (promise && result === errorObj) {
  2885. promise._rejectCallback(result.e, true, true);
  2886. promise = null;
  2887. }
  2888. function resolve(value) {
  2889. if (!promise) return;
  2890. promise._resolveCallback(value);
  2891. promise = null;
  2892. }
  2893. function reject(reason) {
  2894. if (!promise) return;
  2895. promise._rejectCallback(reason, synchronous, true);
  2896. promise = null;
  2897. }
  2898. return ret;
  2899. }
  2900. return tryConvertToPromise;
  2901. };
  2902. },{"./util":21}],21:[function(_dereq_,module,exports){
  2903. "use strict";
  2904. var es5 = _dereq_("./es5");
  2905. var canEvaluate = typeof navigator == "undefined";
  2906. var errorObj = {e: {}};
  2907. var tryCatchTarget;
  2908. var globalObject = typeof self !== "undefined" ? self :
  2909. typeof window !== "undefined" ? window :
  2910. typeof global !== "undefined" ? global :
  2911. this !== undefined ? this : null;
  2912. function tryCatcher() {
  2913. try {
  2914. var target = tryCatchTarget;
  2915. tryCatchTarget = null;
  2916. return target.apply(this, arguments);
  2917. } catch (e) {
  2918. errorObj.e = e;
  2919. return errorObj;
  2920. }
  2921. }
  2922. function tryCatch(fn) {
  2923. tryCatchTarget = fn;
  2924. return tryCatcher;
  2925. }
  2926. var inherits = function(Child, Parent) {
  2927. var hasProp = {}.hasOwnProperty;
  2928. function T() {
  2929. this.constructor = Child;
  2930. this.constructor$ = Parent;
  2931. for (var propertyName in Parent.prototype) {
  2932. if (hasProp.call(Parent.prototype, propertyName) &&
  2933. propertyName.charAt(propertyName.length-1) !== "$"
  2934. ) {
  2935. this[propertyName + "$"] = Parent.prototype[propertyName];
  2936. }
  2937. }
  2938. }
  2939. T.prototype = Parent.prototype;
  2940. Child.prototype = new T();
  2941. return Child.prototype;
  2942. };
  2943. function isPrimitive(val) {
  2944. return val == null || val === true || val === false ||
  2945. typeof val === "string" || typeof val === "number";
  2946. }
  2947. function isObject(value) {
  2948. return typeof value === "function" ||
  2949. typeof value === "object" && value !== null;
  2950. }
  2951. function maybeWrapAsError(maybeError) {
  2952. if (!isPrimitive(maybeError)) return maybeError;
  2953. return new Error(safeToString(maybeError));
  2954. }
  2955. function withAppended(target, appendee) {
  2956. var len = target.length;
  2957. var ret = new Array(len + 1);
  2958. var i;
  2959. for (i = 0; i < len; ++i) {
  2960. ret[i] = target[i];
  2961. }
  2962. ret[i] = appendee;
  2963. return ret;
  2964. }
  2965. function getDataPropertyOrDefault(obj, key, defaultValue) {
  2966. if (es5.isES5) {
  2967. var desc = Object.getOwnPropertyDescriptor(obj, key);
  2968. if (desc != null) {
  2969. return desc.get == null && desc.set == null
  2970. ? desc.value
  2971. : defaultValue;
  2972. }
  2973. } else {
  2974. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  2975. }
  2976. }
  2977. function notEnumerableProp(obj, name, value) {
  2978. if (isPrimitive(obj)) return obj;
  2979. var descriptor = {
  2980. value: value,
  2981. configurable: true,
  2982. enumerable: false,
  2983. writable: true
  2984. };
  2985. es5.defineProperty(obj, name, descriptor);
  2986. return obj;
  2987. }
  2988. function thrower(r) {
  2989. throw r;
  2990. }
  2991. var inheritedDataKeys = (function() {
  2992. var excludedPrototypes = [
  2993. Array.prototype,
  2994. Object.prototype,
  2995. Function.prototype
  2996. ];
  2997. var isExcludedProto = function(val) {
  2998. for (var i = 0; i < excludedPrototypes.length; ++i) {
  2999. if (excludedPrototypes[i] === val) {
  3000. return true;
  3001. }
  3002. }
  3003. return false;
  3004. };
  3005. if (es5.isES5) {
  3006. var getKeys = Object.getOwnPropertyNames;
  3007. return function(obj) {
  3008. var ret = [];
  3009. var visitedKeys = Object.create(null);
  3010. while (obj != null && !isExcludedProto(obj)) {
  3011. var keys;
  3012. try {
  3013. keys = getKeys(obj);
  3014. } catch (e) {
  3015. return ret;
  3016. }
  3017. for (var i = 0; i < keys.length; ++i) {
  3018. var key = keys[i];
  3019. if (visitedKeys[key]) continue;
  3020. visitedKeys[key] = true;
  3021. var desc = Object.getOwnPropertyDescriptor(obj, key);
  3022. if (desc != null && desc.get == null && desc.set == null) {
  3023. ret.push(key);
  3024. }
  3025. }
  3026. obj = es5.getPrototypeOf(obj);
  3027. }
  3028. return ret;
  3029. };
  3030. } else {
  3031. var hasProp = {}.hasOwnProperty;
  3032. return function(obj) {
  3033. if (isExcludedProto(obj)) return [];
  3034. var ret = [];
  3035. /*jshint forin:false */
  3036. enumeration: for (var key in obj) {
  3037. if (hasProp.call(obj, key)) {
  3038. ret.push(key);
  3039. } else {
  3040. for (var i = 0; i < excludedPrototypes.length; ++i) {
  3041. if (hasProp.call(excludedPrototypes[i], key)) {
  3042. continue enumeration;
  3043. }
  3044. }
  3045. ret.push(key);
  3046. }
  3047. }
  3048. return ret;
  3049. };
  3050. }
  3051. })();
  3052. var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
  3053. function isClass(fn) {
  3054. try {
  3055. if (typeof fn === "function") {
  3056. var keys = es5.names(fn.prototype);
  3057. var hasMethods = es5.isES5 && keys.length > 1;
  3058. var hasMethodsOtherThanConstructor = keys.length > 0 &&
  3059. !(keys.length === 1 && keys[0] === "constructor");
  3060. var hasThisAssignmentAndStaticMethods =
  3061. thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
  3062. if (hasMethods || hasMethodsOtherThanConstructor ||
  3063. hasThisAssignmentAndStaticMethods) {
  3064. return true;
  3065. }
  3066. }
  3067. return false;
  3068. } catch (e) {
  3069. return false;
  3070. }
  3071. }
  3072. function toFastProperties(obj) {
  3073. /*jshint -W027,-W055,-W031*/
  3074. function FakeConstructor() {}
  3075. FakeConstructor.prototype = obj;
  3076. var l = 8;
  3077. while (l--) new FakeConstructor();
  3078. return obj;
  3079. eval(obj);
  3080. }
  3081. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  3082. function isIdentifier(str) {
  3083. return rident.test(str);
  3084. }
  3085. function filledRange(count, prefix, suffix) {
  3086. var ret = new Array(count);
  3087. for(var i = 0; i < count; ++i) {
  3088. ret[i] = prefix + i + suffix;
  3089. }
  3090. return ret;
  3091. }
  3092. function safeToString(obj) {
  3093. try {
  3094. return obj + "";
  3095. } catch (e) {
  3096. return "[no string representation]";
  3097. }
  3098. }
  3099. function isError(obj) {
  3100. return obj !== null &&
  3101. typeof obj === "object" &&
  3102. typeof obj.message === "string" &&
  3103. typeof obj.name === "string";
  3104. }
  3105. function markAsOriginatingFromRejection(e) {
  3106. try {
  3107. notEnumerableProp(e, "isOperational", true);
  3108. }
  3109. catch(ignore) {}
  3110. }
  3111. function originatesFromRejection(e) {
  3112. if (e == null) return false;
  3113. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  3114. e["isOperational"] === true);
  3115. }
  3116. function canAttachTrace(obj) {
  3117. return isError(obj) && es5.propertyIsWritable(obj, "stack");
  3118. }
  3119. var ensureErrorObject = (function() {
  3120. if (!("stack" in new Error())) {
  3121. return function(value) {
  3122. if (canAttachTrace(value)) return value;
  3123. try {throw new Error(safeToString(value));}
  3124. catch(err) {return err;}
  3125. };
  3126. } else {
  3127. return function(value) {
  3128. if (canAttachTrace(value)) return value;
  3129. return new Error(safeToString(value));
  3130. };
  3131. }
  3132. })();
  3133. function classString(obj) {
  3134. return {}.toString.call(obj);
  3135. }
  3136. function copyDescriptors(from, to, filter) {
  3137. var keys = es5.names(from);
  3138. for (var i = 0; i < keys.length; ++i) {
  3139. var key = keys[i];
  3140. if (filter(key)) {
  3141. try {
  3142. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  3143. } catch (ignore) {}
  3144. }
  3145. }
  3146. }
  3147. var asArray = function(v) {
  3148. if (es5.isArray(v)) {
  3149. return v;
  3150. }
  3151. return null;
  3152. };
  3153. if (typeof Symbol !== "undefined" && Symbol.iterator) {
  3154. var ArrayFrom = typeof Array.from === "function" ? function(v) {
  3155. return Array.from(v);
  3156. } : function(v) {
  3157. var ret = [];
  3158. var it = v[Symbol.iterator]();
  3159. var itResult;
  3160. while (!((itResult = it.next()).done)) {
  3161. ret.push(itResult.value);
  3162. }
  3163. return ret;
  3164. };
  3165. asArray = function(v) {
  3166. if (es5.isArray(v)) {
  3167. return v;
  3168. } else if (v != null && typeof v[Symbol.iterator] === "function") {
  3169. return ArrayFrom(v);
  3170. }
  3171. return null;
  3172. };
  3173. }
  3174. var isNode = typeof process !== "undefined" &&
  3175. classString(process).toLowerCase() === "[object process]";
  3176. function env(key, def) {
  3177. return isNode ? process.env[key] : def;
  3178. }
  3179. var ret = {
  3180. isClass: isClass,
  3181. isIdentifier: isIdentifier,
  3182. inheritedDataKeys: inheritedDataKeys,
  3183. getDataPropertyOrDefault: getDataPropertyOrDefault,
  3184. thrower: thrower,
  3185. isArray: es5.isArray,
  3186. asArray: asArray,
  3187. notEnumerableProp: notEnumerableProp,
  3188. isPrimitive: isPrimitive,
  3189. isObject: isObject,
  3190. isError: isError,
  3191. canEvaluate: canEvaluate,
  3192. errorObj: errorObj,
  3193. tryCatch: tryCatch,
  3194. inherits: inherits,
  3195. withAppended: withAppended,
  3196. maybeWrapAsError: maybeWrapAsError,
  3197. toFastProperties: toFastProperties,
  3198. filledRange: filledRange,
  3199. toString: safeToString,
  3200. canAttachTrace: canAttachTrace,
  3201. ensureErrorObject: ensureErrorObject,
  3202. originatesFromRejection: originatesFromRejection,
  3203. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  3204. classString: classString,
  3205. copyDescriptors: copyDescriptors,
  3206. hasDevTools: typeof chrome !== "undefined" && chrome &&
  3207. typeof chrome.loadTimes === "function",
  3208. isNode: isNode,
  3209. env: env,
  3210. global: globalObject
  3211. };
  3212. ret.isRecentNode = ret.isNode && (function() {
  3213. var version = process.versions.node.split(".").map(Number);
  3214. return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
  3215. })();
  3216. if (ret.isNode) ret.toFastProperties(process);
  3217. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  3218. module.exports = ret;
  3219. },{"./es5":10}]},{},[3])(3)
  3220. }); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; }