44
55class When
66{
7+ public static function resolve ($ promiseOrValue )
8+ {
9+ $ deferred = new Deferred ();
10+ $ deferred ->resolve ($ promiseOrValue );
11+
12+ return $ deferred ->promise ();
13+ }
14+
15+ public static function reject ($ promiseOrValue )
16+ {
17+ return static ::resolve ($ promiseOrValue )->then (function ($ value = null ) {
18+ return new RejectedPromise ($ value );
19+ });
20+ }
21+
722 public static function all ($ promisesOrValues , $ fulfilledHandler = null , $ errorHandler = null , $ progressHandler = null )
823 {
924 $ promise = static ::map ($ promisesOrValues , function ($ val ) {
@@ -26,7 +41,7 @@ public static function any($promisesOrValues, $fulfilledHandler = null, $errorHa
2641
2742 public static function some ($ promisesOrValues , $ howMany , $ fulfilledHandler = null , $ errorHandler = null , $ progressHandler = null )
2843 {
29- return Util ::resolve ($ promisesOrValues )->then (function ($ array ) use ($ howMany , $ fulfilledHandler , $ errorHandler , $ progressHandler ) {
44+ return When ::resolve ($ promisesOrValues )->then (function ($ array ) use ($ howMany , $ fulfilledHandler , $ errorHandler , $ progressHandler ) {
3045 if (!is_array ($ array )) {
3146 $ array = array ();
3247 }
@@ -79,7 +94,7 @@ public static function some($promisesOrValues, $howMany, $fulfilledHandler = nul
7994 }
8095 };
8196
82- Util ::resolve ($ promiseOrValue )->then ($ fulfiller , $ rejecter , $ progress );
97+ When ::resolve ($ promiseOrValue )->then ($ fulfiller , $ rejecter , $ progress );
8398 }
8499 }
85100
@@ -89,7 +104,7 @@ public static function some($promisesOrValues, $howMany, $fulfilledHandler = nul
89104
90105 public static function map ($ promisesOrValues , $ mapFunc )
91106 {
92- return Util ::resolve ($ promisesOrValues )->then (function ($ array ) use ($ mapFunc ) {
107+ return When ::resolve ($ promisesOrValues )->then (function ($ array ) use ($ mapFunc ) {
93108 if (!is_array ($ array )) {
94109 $ array = array ();
95110 }
@@ -102,7 +117,7 @@ public static function map($promisesOrValues, $mapFunc)
102117 $ deferred ->resolve ($ results );
103118 } else {
104119 $ resolve = function ($ item , $ i ) use ($ mapFunc , &$ results , &$ toResolve , $ deferred ) {
105- Util ::resolve ($ item )
120+ When ::resolve ($ item )
106121 ->then ($ mapFunc )
107122 ->then (
108123 function ($ mapped ) use (&$ results , $ i , &$ toResolve , $ deferred ) {
@@ -127,7 +142,7 @@ function ($mapped) use (&$results, $i, &$toResolve, $deferred) {
127142
128143 public static function reduce ($ promisesOrValues , $ reduceFunc , $ initialValue = null )
129144 {
130- return Util ::resolve ($ promisesOrValues )->then (function ($ array ) use ($ reduceFunc , $ initialValue ) {
145+ return When ::resolve ($ promisesOrValues )->then (function ($ array ) use ($ reduceFunc , $ initialValue ) {
131146 if (!is_array ($ array )) {
132147 $ array = array ();
133148 }
@@ -138,8 +153,8 @@ public static function reduce($promisesOrValues, $reduceFunc , $initialValue = n
138153 // Wrap the supplied $reduceFunc with one that handles promises and then
139154 // delegates to the supplied.
140155 $ wrappedReduceFunc = function ($ current , $ val ) use ($ reduceFunc , $ total , &$ i ) {
141- return Util ::resolve ($ current )->then (function ($ c ) use ($ reduceFunc , $ total , &$ i , $ val ) {
142- return Util ::resolve ($ val )->then (function ($ value ) use ($ reduceFunc , $ total , &$ i , $ c ) {
156+ return When ::resolve ($ current )->then (function ($ c ) use ($ reduceFunc , $ total , &$ i , $ val ) {
157+ return When ::resolve ($ val )->then (function ($ value ) use ($ reduceFunc , $ total , &$ i , $ c ) {
143158 return call_user_func ($ reduceFunc , $ c , $ value , $ i ++, $ total );
144159 });
145160 });
0 commit comments