module promise pub interface JS.Promise { then(onFullfilled JS.Any, onRejected JS.Any) catch(onCatch JS.Any) JS.Promise finally(callback JS.Any) JS.Promise } [use_new] pub fn JS.Promise.prototype.constructor(JS.Any) JS.Promise pub fn JS.Promise.reject(JS.Any) JS.Promise pub fn JS.Promise.resolve(JS.Any) JS.Promise pub fn JS.Promise.race(JS.Array) JS.Promise // The Promise object represents the eventual completion (or failure) // of an asynchronous operation and its resulting value. pub struct Promise { mut: promise JS.Promise [noinit] } pub fn new(executor fn (resolve fn (T), reject fn (E))) Promise { promise := JS.Promise.prototype.constructor(executor) return Promise{promise} } pub fn (p Promise) then(on_fullfilled fn (T), on_rejected fn (E)) { p.promise.then(on_fullfilled, on_rejected) } // catch method returns a Promise and deals with rejected cases only. pub fn (p Promise) catch(callback fn (error JS.Any)) Promise { promise := p.promise.catch(callback) return Promise{promise} } pub fn (p Promise) finally(callback fn ()) Promise { promise := p.promise.finally(callback) return Promise{promise} } // reject returns promise which was rejected because of specified error pub fn reject(error E) Promise { promise := JS.Promise.reject(error) return Promise{promise} } // resolve returns promise which was resolved with specified value pub fn resolve(result T) Promise { promise := JS.Promise.resolve(error) return Promise{promise} } // race returns returns a promise that fulfills or rejects as soon as one of // the promises in an iterable fulfills or rejects, with the value or reason from that promise. pub fn race(promises []Promise) Promise { promises_ := JS.Array.prototype.constructor() for elem in promises { promises_.push(elem.promise) } promise := JS.Promise.race(promises_) return Promise{promise} } pub fn JS.Promise.all(JS.Array) JS.Promise pub fn JS.Promise.allSettled(JS.Array) JS.Promise /* // 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 pub fn all(array []JS.Promise) Promise { mut promise := JS.Promise(JS.Any(voidptr(0))) #promise = Promise.all(array.arr.arr); return Promise{promise} } */