mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:57:35 +00:00
LibCore: Check if a promise is already resolved in Promise::map()
This commit is contained in:
parent
50d8ef94ba
commit
51fefb57fc
2 changed files with 42 additions and 2 deletions
|
@ -60,9 +60,15 @@ public:
|
|||
|
||||
// Converts a Promise<A> to a Promise<B> using a function func: A -> B
|
||||
template<typename T>
|
||||
RefPtr<Promise<T>> map(Function<T(Result&)> func)
|
||||
NonnullRefPtr<Promise<T>> map(Function<T(Result&)> func)
|
||||
{
|
||||
RefPtr<Promise<T>> new_promise = Promise<T>::construct();
|
||||
NonnullRefPtr<Promise<T>> new_promise = Promise<T>::construct();
|
||||
|
||||
if (is_resolved())
|
||||
new_promise->resolve(func(m_result_or_rejection->value()));
|
||||
if (is_rejected())
|
||||
new_promise->reject(m_result_or_rejection->release_error());
|
||||
|
||||
on_resolution = [new_promise, func = move(func)](Result& result) -> ErrorOr<void> {
|
||||
new_promise->resolve(func(result));
|
||||
return {};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue