| 1 | module builtin |
| 2 | |
| 3 | pub interface JS.Promise { |
| 4 | then(onFullfiled JS.Any, onRejected JS.Any) |
| 5 | catch(onCatch JS.Any) JS.Promise |
| 6 | finally(callback JS.Any) JS.Promise |
| 7 | } |
| 8 | |
| 9 | @[use_new] |
| 10 | pub fn JS.Promise.prototype.constructor(JS.Any) JS.Promise |
| 11 | pub fn JS.Promise.reject(JS.Any) JS.Promise |
| 12 | pub fn JS.Promise.resolve(JS.Any) JS.Promise |
| 13 | pub fn JS.Promise.race(JS.Array) JS.Promise |
| 14 | |
| 15 | // Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value. |
| 16 | pub struct Promise[T] { |
| 17 | pub mut: |
| 18 | promise JS.Promise @[noinit] |
| 19 | } |
| 20 | |
| 21 | pub fn promise_new[T](executor fn (resolve fn (T), reject fn (JS.Any))) Promise[T] { |
| 22 | promise := JS.Promise.prototype.constructor(executor) |
| 23 | return Promise[T]{promise} |
| 24 | } |
| 25 | |
| 26 | pub fn (p Promise[T]) then(on_fulfilled fn (T), on_rejected fn (JS.Any)) { |
| 27 | p.promise.then(on_fulfilled, on_rejected) |
| 28 | } |
| 29 | |
| 30 | // catch returns a Promise and deals with rejected cases only. |
| 31 | pub fn (p Promise[T]) catch(callback fn (error JS.Any)) Promise[T] { |
| 32 | promise := p.promise.catch(callback) |
| 33 | return Promise[T]{promise} |
| 34 | } |
| 35 | |
| 36 | pub fn (p Promise[T]) finally[U](callback fn ()) Promise[JS.Any] { |
| 37 | promise := p.promise.finally(callback) |
| 38 | return Promise[JS.Any]{promise} |
| 39 | } |
| 40 | |
| 41 | // promise_reject returns promise which was rejected because of specified error. |
| 42 | pub fn promise_reject(error JS.Any) Promise[JS.Any] { |
| 43 | promise := JS.Promise.reject(error) |
| 44 | return Promise[JS.Any]{promise} |
| 45 | } |
| 46 | |
| 47 | // resolve returns promise which was resolved with specified value. |
| 48 | pub fn promise_resolve[T](result T) Promise[T] { |
| 49 | promise := JS.Promise.resolve(result) |
| 50 | return Promise[T]{promise} |
| 51 | } |
| 52 | |
| 53 | // promise_race returns a promise, that fulfills or rejects, as soon as one of the promises in an iterable fulfills or rejects. |
| 54 | // It returns the value or reason from that first promise. |
| 55 | pub fn promise_race[T](promises []Promise[T]) Promise[T] { |
| 56 | promises_ := JS.Array.prototype.constructor() |
| 57 | |
| 58 | for elem in promises { |
| 59 | promises_.push(elem.promise) |
| 60 | } |
| 61 | |
| 62 | promise := JS.Promise.race(promises_) |
| 63 | return Promise[T]{promise} |
| 64 | } |
| 65 | |
| 66 | pub fn JS.Promise.all(JS.Array) JS.Promise |
| 67 | pub fn JS.Promise.allSettled(JS.Array) JS.Promise |
| 68 | |
| 69 | /* |
| 70 | pub type JsAny = JS.Any |
| 71 | |
| 72 | // all takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. |
| 73 | pub fn all(array []JS.Promise) Promise<JS.Array, js.promise.JsAny> { |
| 74 | mut promise := JS.Promise(JS.Any(voidptr(0))) |
| 75 | #promise = Promise.all(array.arr.arr); |
| 76 | |
| 77 | return Promise<JS.Array,JsAny>{promise} |
| 78 | } |
| 79 | */ |
| 80 | |