2025-01-22 16:18:30 +01:00
/ *
* Copyright ( C ) 2005 - 2019 Haxe Foundation
*
* Permission is hereby granted , free of charge , to any person obtaining a
* copy of this software and associated documentation files ( the " S o f t w a r e " ) ,
* to deal in the Software without restriction , including without limitation
* the rights to use , copy , modify , merge , publish , distribute , sublicense ,
* and / or sell copies of the Software , and to permit persons to whom the
* Software is furnished to do so , s u b j e c t t o t h e f o l l o w i n g c o n d i t i o n s :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " A S I S " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING
* FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE .
* /
package js . lib ;
import haxe . extern . EitherType ;
/ * *
The Promise object represents the eventual completion ( or f a i l u r e ) of an
asynchronous operation and its resulting value .
Documentation [ Promise ] ( https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
* * /
@ : native ( " P r o m i s e " )
extern class Promise < T > {
/ * *
Returns a Promise object that is resolved with the given value . If the
value is Thenable , the returned promise will " f o l l o w " that
thenable , adopting its eventual state ;
otherwise the returned promise will be fulfilled with the value .
Generally , when it ' s u n k n o w n w h e n v a l u e i s a p r o m i s e o r n o t ,
use ` Promise . resolve ( value ) ` instead and work with the return value as
a promise .
* * /
@ : overload ( function < T > ( ? value : T ) : Promise < T > { } )
static function resolve < T > ( thenable : Thenable < T > ) : Promise < T > ;
/ * *
Returns a Promise object that is rejected with the given reason .
* * /
static function reject < T > ( ? reason : Dynamic ) : Promise < T > ;
/ * *
Returns a promise that either fulfills when all of the promises in the
iterable argument have fulfilled or rejects as soon as one of the
promises in the iterable argument rejects . If the returned promise
fulfills , it is fulfilled with an array of the values from the
fulfilled promises in the same order as defined in the iterable .
If the returned promise rejects , it is rejected with the reason from
the first promise in the iterable that rejected . This method can be
useful for a g g r e g a t i n g r e s u l t s o f m u l t i p l e p r o m i s e s .
* * /
2026-05-21 23:40:20 -07:00
@ : overload ( function ( iterable : Array < Dynamic > ) : Promise < Array < Dynamic > > { } )
static function all < T > ( iterable : Array < Promise < T > > ) : Promise < Array < T > > ;
2025-01-22 16:18:30 +01:00
/ * *
Returns a promise that resolves after all of the given promises have either fulfilled or rejected ,
with an array of objects that each describes the outcome of each promise .
It is typically used when you have multiple asynchronous tasks that are not dependent on one another
to complete successfully , or you ' d a l w a y s l i k e t o k n o w t h e r e s u l t o f e a c h p r o m i s e .
In comparison , the Promise returned by ` Promise . all ` may be more appropriate if t h e t a s k s a r e d e p e n d e n t
on each other / if y o u ' d l i k e t o i m m e d i a t e l y r e j e c t u p o n a n y o f t h e m r e j e c t i n g .
* * /
2026-05-21 23:40:20 -07:00
@ : overload ( function ( iterable : Array < Dynamic > ) : Promise < Array < PromiseSettleOutcome < Dynamic > > > { } )
static function allSettled < T > ( iterable : Array < Promise < T > > ) : Promise < Array < PromiseSettleOutcome < T > > > ;
2025-01-22 16:18:30 +01:00
/ * *
Returns a promise that fulfills or rejects as soon as one of the
promises in the iterable fulfills or rejects , with the value or reason
from that promise .
* * /
2026-05-21 23:40:20 -07:00
@ : overload ( function ( iterable : Array < Dynamic > ) : Promise < Dynamic > { } )
static function race < T > ( iterable : Array < Promise < T > > ) : Promise < T > ;
2025-01-22 16:18:30 +01:00
/** @throws DOMError */
function n e w ( init : ( resolve : ( v a l u e : T ) -> Void , reject : ( reason : D y n a m i c ) -> Void ) - > Void ) : Void ;
/ * *
Appends fulfillment and rejection handlers to the promise and returns a
new promise r e s o l v i n g t o t h e r e t u r n v a l u e o f t h e c a l l e d h a n d l e r , o r t o
its original settled value if t h e p r o m i s e w a s n o t h a n d l e d
( i . e . i f t h e r e l e v a n t h a n d l e r o n F u l f i l l e d o r o n R e j e c t e d i s n o t a f u n c t i o n ) .
* * /
function then < TOut > ( onFulfilled : Null < PromiseHandler < T , TOut > > , ? onRejected : PromiseHandler < Dynamic , TOut > ) : Promise < TOut > ;
/ * *
Appends a rejection handler callback to the promise , and returns a new
promise resolving to the return value of the callback if i t i s c a l l e d ,
or to its original fulfillment value if t h e p r o m i s e i s i n s t e a d f u l f i l l e d .
* * /
@ : native ( " c a t c h " )
@ : overload ( function < TOut > ( onRejected : PromiseHandler < Dynamic , TOut > ) : Promise < EitherType < T , TOut > > { } )
function catchError ( onRejected : PromiseHandler < Dynamic , T > ) : Promise < T > ;
/ * *
Returns a Promise . When the promise is settled , i . e either fulfilled or rejected ,
the specified callback function is e x e c u t e d . T h i s p r o v i d e s a w a y f o r c o d e t o b e r u n
whether the promise was fulfilled successfully or rejected once the Promise has been dealt with .
* * /
function finally ( onFinally : ( ) - > Void ) : P r o m i s e < T > ;
}
/ * *
Handler type for t h e P r o m i s e o b j e c t .
* * /
abstract PromiseHandler < T , TOut > ( T -> Dynamic ) // T->Dynamic, so the compiler always knows the type of the argument and can infer it for then/catch callbacks
from T - > Promise < TOut > // support Promise explicitly as it doesn't work transitively through Thenable at the moment
from T - > Thenable < TOut > // although the checking order seems to be reversed at the moment, see https://github.com/HaxeFoundation/haxe/issues/7656
from T - > TOut // order is important, because Promise<TOut> return must have priority
{ }
/ * *
A value with a ` then ` method .
* * /
@ : forward
@ : transitive
abstract Thenable < T > ( ThenableStruct < T > )
from ThenableStruct < T > { } // abstract wrapping prevents compiler hanging, see https://github.com/HaxeFoundation/haxe/issues/5785
typedef ThenableStruct < T > = {
function then < TOut > ( onFulfilled : Null < PromiseHandler < T , TOut > > , ? onRejected : PromiseHandler < Dynamic , TOut > ) : Thenable < TOut > ;
}
2026-05-21 23:40:20 -07:00
typedef PromiseSettleOutcome < T > = {
2025-01-22 16:18:30 +01:00
var status: PromiseSettleStatus ;
2026-05-21 23:40:20 -07:00
var ? value: T ;
2025-01-22 16:18:30 +01:00
var ? reason: Dynamic ;
}
enum a b s t r a c t PromiseSettleStatus ( S t r i n g ) t o S t r i n g {
var Fulfilled = " f u l f i l l e d " ;
var Rejected = " r e j e c t e d " ;
}