easy-promise.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*!
  2. * easy-promise v0.0.3
  3. * Homepage https://github.com/cnwhy/promise#readme
  4. * License BSD-2-Clause
  5. */
  6. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  7. (function (name, factory) {
  8. if (typeof define === 'function' && (define.amd || define.cmd)) {
  9. define([], factory);
  10. } else if (typeof window !== "undefined" || typeof self !== "undefined") {
  11. var global = typeof window !== "undefined" ? window : self;
  12. global[name] = factory();
  13. } else {
  14. throw new Error("加载 " + name + " 模块失败!,请检查您的环境!")
  15. }
  16. })('Promise',function(){
  17. var Promise = require("./setTimeout");
  18. return Promise;
  19. });
  20. },{"./setTimeout":2}],2:[function(require,module,exports){
  21. module.exports = require("./src")(function(fn){setTimeout(fn,0)});
  22. },{"./src":3}],3:[function(require,module,exports){
  23. "use strict";
  24. module.exports = function(nextTick){
  25. var FUN = function(){};
  26. function Resolve(promise, x) {
  27. if(isPromise(x)){
  28. x.then(promise.resolve,promise.reject)
  29. }else if (x && (typeof x === 'function' || typeof x === 'object')) {
  30. var called = false,then;
  31. try {
  32. then = x.then;
  33. if (typeof then === 'function') {
  34. then.call(x, function(y) {
  35. if (called) return;
  36. called = true;
  37. Resolve(promise, y);
  38. }, function(r) {
  39. if (called) return;
  40. called = true;
  41. promise.reject(r);
  42. });
  43. }else {
  44. promise.resolve(x);
  45. }
  46. }catch (e) {
  47. if (!called) {
  48. called = true;
  49. promise.reject(e);
  50. }
  51. }
  52. }else {
  53. promise.resolve(x);
  54. }
  55. }
  56. function isPromise(obj){
  57. return obj instanceof Promise_;
  58. }
  59. function bind(fun,self){
  60. var arg = Array.prototype.slice.call(arguments,2);
  61. return function(){
  62. fun.apply(self,arg.concat(Array.prototype.slice.call(arguments)));
  63. }
  64. }
  65. function Promise_(fun){
  66. //var defer = this.defer = new Defer(this);
  67. var self = this;
  68. this.status = -1; //pending:-1 ; fulfilled:1 ; rejected:0
  69. this._events = [];
  70. var lock = false;
  71. function _resolve(value){
  72. changeStatus.call(self,1,value)
  73. }
  74. function _reject(reason){
  75. changeStatus.call(self,0,reason)
  76. }
  77. function resolve(value){
  78. if(lock) return;
  79. lock = true;
  80. if(self === value){
  81. return _reject(new TypeError("The promise and its value refer to the same object"));
  82. }
  83. Resolve({resolve:_resolve,reject:_reject},value)
  84. }
  85. function reject(reason){
  86. if(lock) return;
  87. lock = true;
  88. _reject(reason);
  89. }
  90. this.resolve = resolve;
  91. this.reject = reject;
  92. if(fun !== FUN && typeof fun == "function"){
  93. try{
  94. fun(this.resolve,this.reject);
  95. }catch(e){
  96. this.reject(e)
  97. }
  98. }
  99. }
  100. Promise_.defer = function(){
  101. var _promise = new Promise_(FUN);
  102. return {
  103. promise: _promise,
  104. resolve: _promise.resolve,
  105. reject: _promise.reject
  106. }
  107. }
  108. Promise_.resolve = function(obj){
  109. if(isPromise(obj)) return obj;
  110. return new Promise_(function(ok,no){
  111. ok(obj);
  112. })
  113. }
  114. Promise_.reject = function(err){
  115. return new Promise_(function(ok,no){
  116. no(err);
  117. })
  118. }
  119. Promise.prototype.toString = function () {
  120. return "[object Promise]";
  121. }
  122. Promise_.prototype.then = function(ok,no){
  123. var status = this.status;
  124. var defer = Promise_.defer()
  125. ,promise = defer.promise
  126. if(!~status){
  127. this._events.push([ok,no,promise]);
  128. }else if(status && typeof ok == "function"){
  129. runThen(ok,this.value,promise,status);
  130. }else if(!status && typeof no == "function"){
  131. runThen(no,this.reason,promise,status)
  132. }else{
  133. if(status) defer.resolve(this.value)
  134. else defer.reject(this.reason);
  135. }
  136. // this._events.push([ok,no,promise]);
  137. // runThens.call(this)
  138. return promise;
  139. }
  140. function changeStatus(status,arg){
  141. var self = this;
  142. if(~this.status) return;
  143. this.status = status;
  144. if(status){
  145. this.value = arg
  146. }else{
  147. this.reason = arg;
  148. }
  149. runThens.call(self)
  150. }
  151. function runThens(){
  152. if(!~this.status) return;
  153. var self = this
  154. ,_event = self._events
  155. ,arg = self.status ? self.value : self.reason
  156. ,FnNumb = self.status ? 0 : 1;
  157. //while(_event.length){
  158. for(var i=0; i<_event.length; i++){
  159. (function(eArr){
  160. var resolve,reject
  161. var fn = eArr[FnNumb]
  162. ,nextQ = eArr[2]
  163. runThen(fn,arg,nextQ,self.status);
  164. })(_event[i])
  165. // })(_event.shift())
  166. }
  167. _event = [];
  168. }
  169. function runThen(fn,arg,nextQ,status){
  170. var resolve = nextQ.resolve
  171. ,reject = nextQ.reject
  172. // if(nextQ){
  173. // resolve = nextQ.resolve
  174. // reject = nextQ.reject
  175. // }
  176. if(typeof fn == 'function'){
  177. nextTick(function(){
  178. var nextPromise;
  179. try{
  180. nextPromise = fn(arg)
  181. }catch(e){
  182. reject(e)
  183. // if(reject)
  184. // else throw e;
  185. return;
  186. }
  187. resolve(nextPromise);
  188. })
  189. }else{
  190. if (status) resolve(arg)
  191. else reject(arg)
  192. }
  193. }
  194. return Promise_;
  195. }
  196. },{}]},{},[1])