Promise.hx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib;
  23. import haxe.extern.EitherType;
  24. /**
  25. The Promise object represents the eventual completion (or failure) of an
  26. asynchronous operation and its resulting value.
  27. 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/).
  28. **/
  29. @:native("Promise")
  30. extern class Promise<T> {
  31. /**
  32. Returns a Promise object that is resolved with the given value. If the
  33. value is Thenable, the returned promise will "follow" that
  34. thenable, adopting its eventual state;
  35. otherwise the returned promise will be fulfilled with the value.
  36. Generally, when it's unknown when value is a promise or not,
  37. use `Promise.resolve(value)` instead and work with the return value as
  38. a promise.
  39. **/
  40. @:overload(function<T>(?value:T):Promise<T> {})
  41. static function resolve<T>(thenable:Thenable<T>):Promise<T>;
  42. /**
  43. Returns a Promise object that is rejected with the given reason.
  44. **/
  45. static function reject<T>(?reason:Dynamic):Promise<T>;
  46. /**
  47. Returns a promise that either fulfills when all of the promises in the
  48. iterable argument have fulfilled or rejects as soon as one of the
  49. promises in the iterable argument rejects. If the returned promise
  50. fulfills, it is fulfilled with an array of the values from the
  51. fulfilled promises in the same order as defined in the iterable.
  52. If the returned promise rejects, it is rejected with the reason from
  53. the first promise in the iterable that rejected. This method can be
  54. useful for aggregating results of multiple promises.
  55. **/
  56. @:overload(function(iterable:Array<Dynamic>):Promise<Array<Dynamic>> {})
  57. static function all<T>(iterable:Array<Promise<T>>):Promise<Array<T>>;
  58. /**
  59. Returns a promise that resolves after all of the given promises have either fulfilled or rejected,
  60. with an array of objects that each describes the outcome of each promise.
  61. It is typically used when you have multiple asynchronous tasks that are not dependent on one another
  62. to complete successfully, or you'd always like to know the result of each promise.
  63. In comparison, the Promise returned by `Promise.all` may be more appropriate if the tasks are dependent
  64. on each other / if you'd like to immediately reject upon any of them rejecting.
  65. **/
  66. @:overload(function(iterable:Array<Dynamic>):Promise<Array<PromiseSettleOutcome<Dynamic>>> {})
  67. static function allSettled<T>(iterable:Array<Promise<T>>):Promise<Array<PromiseSettleOutcome<T>>>;
  68. /**
  69. Returns a promise that fulfills or rejects as soon as one of the
  70. promises in the iterable fulfills or rejects, with the value or reason
  71. from that promise.
  72. **/
  73. @:overload(function(iterable:Array<Dynamic>):Promise<Dynamic> {})
  74. static function race<T>(iterable:Array<Promise<T>>):Promise<T>;
  75. /** @throws DOMError */
  76. function new(init:(resolve:(value:T) -> Void, reject:(reason:Dynamic) -> Void) -> Void):Void;
  77. /**
  78. Appends fulfillment and rejection handlers to the promise and returns a
  79. new promise resolving to the return value of the called handler, or to
  80. its original settled value if the promise was not handled
  81. (i.e. if the relevant handler onFulfilled or onRejected is not a function).
  82. **/
  83. function then<TOut>(onFulfilled:Null<PromiseHandler<T, TOut>>, ?onRejected:PromiseHandler<Dynamic, TOut>):Promise<TOut>;
  84. /**
  85. Appends a rejection handler callback to the promise, and returns a new
  86. promise resolving to the return value of the callback if it is called,
  87. or to its original fulfillment value if the promise is instead fulfilled.
  88. **/
  89. @:native("catch")
  90. @:overload(function<TOut>(onRejected:PromiseHandler<Dynamic, TOut>):Promise<EitherType<T, TOut>> {})
  91. function catchError(onRejected:PromiseHandler<Dynamic, T>):Promise<T>;
  92. /**
  93. Returns a Promise. When the promise is settled, i.e either fulfilled or rejected,
  94. the specified callback function is executed. This provides a way for code to be run
  95. whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.
  96. **/
  97. function finally(onFinally:() -> Void):Promise<T>;
  98. }
  99. /**
  100. Handler type for the Promise object.
  101. **/
  102. 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
  103. from T->Promise<TOut> // support Promise explicitly as it doesn't work transitively through Thenable at the moment
  104. from T->Thenable<TOut> // although the checking order seems to be reversed at the moment, see https://github.com/HaxeFoundation/haxe/issues/7656
  105. from T->TOut // order is important, because Promise<TOut> return must have priority
  106. {}
  107. /**
  108. A value with a `then` method.
  109. **/
  110. @:forward
  111. @:transitive
  112. abstract Thenable<T>(ThenableStruct<T>)
  113. from ThenableStruct<T> {} // abstract wrapping prevents compiler hanging, see https://github.com/HaxeFoundation/haxe/issues/5785
  114. typedef ThenableStruct<T> = {
  115. function then<TOut>(onFulfilled:Null<PromiseHandler<T, TOut>>, ?onRejected:PromiseHandler<Dynamic, TOut>):Thenable<TOut>;
  116. }
  117. typedef PromiseSettleOutcome<T> = {
  118. var status:PromiseSettleStatus;
  119. var ?value:T;
  120. var ?reason:Dynamic;
  121. }
  122. enum abstract PromiseSettleStatus(String) to String {
  123. var Fulfilled = "fulfilled";
  124. var Rejected = "rejected";
  125. }