1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:07:36 +00:00

LibCore: Check if a promise is already resolved in Promise::map()

This commit is contained in:
Karol Kosek 2023-08-29 14:32:41 +02:00 committed by Andrew Kaster
parent 50d8ef94ba
commit 51fefb57fc
2 changed files with 42 additions and 2 deletions

View file

@ -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 {};