2021-12-07 10:11:54 +01:00
|
|
|
module builtin
|
2021-11-25 15:49:53 +01:00
|
|
|
|
|
|
|
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.
|
2021-12-10 13:54:20 +01:00
|
|
|
pub struct Promise<T> {
|
2021-11-25 15:49:53 +01:00
|
|
|
mut:
|
|
|
|
promise JS.Promise [noinit]
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn promise_new<T>(executor fn (resolve fn (T), reject fn (JS.Any))) Promise<T> {
|
2021-11-25 15:49:53 +01:00
|
|
|
promise := JS.Promise.prototype.constructor(executor)
|
2021-12-10 13:54:20 +01:00
|
|
|
return Promise<T>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn (p Promise<T>) then(on_fullfilled fn (T), on_rejected fn (JS.Any)) {
|
2021-11-25 15:49:53 +01:00
|
|
|
p.promise.then(on_fullfilled, on_rejected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// catch method returns a Promise and deals with rejected cases only.
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn (p Promise<T>) catch(callback fn (error JS.Any)) Promise<T> {
|
2021-11-25 15:49:53 +01:00
|
|
|
promise := p.promise.catch(callback)
|
2021-12-10 13:54:20 +01:00
|
|
|
return Promise<T>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn (p Promise<T>) finally<U>(callback fn ()) Promise<JS.Any> {
|
2021-11-25 15:49:53 +01:00
|
|
|
promise := p.promise.finally(callback)
|
2021-12-10 13:54:20 +01:00
|
|
|
return Promise<JS.Any>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// reject<E> returns promise which was rejected because of specified error
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn promise_reject(error JS.Any) Promise<JS.Any> {
|
2021-11-25 15:49:53 +01:00
|
|
|
promise := JS.Promise.reject(error)
|
2021-12-10 13:54:20 +01:00
|
|
|
return Promise<JS.Any>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolve<E> returns promise which was resolved with specified value
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn promise_resolve<T>(result T) Promise<T> {
|
|
|
|
promise := JS.Promise.resolve(result)
|
|
|
|
return Promise<T>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-12-10 13:54:20 +01:00
|
|
|
pub fn promise_race<T>(promises []Promise<T>) Promise<T> {
|
2021-11-25 15:49:53 +01:00
|
|
|
promises_ := JS.Array.prototype.constructor()
|
|
|
|
|
|
|
|
for elem in promises {
|
|
|
|
promises_.push(elem.promise)
|
|
|
|
}
|
|
|
|
|
|
|
|
promise := JS.Promise.race(promises_)
|
2021-12-10 13:54:20 +01:00
|
|
|
return Promise<T>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn JS.Promise.all(JS.Array) JS.Promise
|
|
|
|
pub fn JS.Promise.allSettled(JS.Array) JS.Promise
|
|
|
|
|
|
|
|
/*
|
2021-11-29 14:32:29 +01:00
|
|
|
pub type JsAny = JS.Any
|
|
|
|
|
2021-11-25 15:49:53 +01:00
|
|
|
// 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
|
2021-11-29 14:32:29 +01:00
|
|
|
pub fn all(array []JS.Promise) Promise<JS.Array, js.promise.JsAny> {
|
2021-11-25 15:49:53 +01:00
|
|
|
mut promise := JS.Promise(JS.Any(voidptr(0)))
|
|
|
|
#promise = Promise.all(array.arr.arr);
|
|
|
|
|
2021-11-29 14:32:29 +01:00
|
|
|
return Promise<JS.Array,JsAny>{promise}
|
2021-11-25 15:49:53 +01:00
|
|
|
}
|
|
|
|
*/
|