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

LibCore+Userland: Make Promise's on_resolve fallible

This will be primarily necessary for BackgroundAction integration, but
it already allows us to add proper error handling in LibIMAP :^)
This commit is contained in:
kleines Filmröllchen 2022-12-29 14:53:47 +01:00 committed by Linus Groh
parent dc318d3080
commit 8f4d0d3797
5 changed files with 24 additions and 20 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,14 +17,15 @@ class Promise : public Object {
C_OBJECT(Promise);
public:
Function<void(Result&)> on_resolved;
Function<ErrorOr<void>(Result&)> on_resolved;
void resolve(Result&& result)
ErrorOr<void> resolve(Result&& result)
{
m_pending_or_error = move(result);
if (on_resolved)
on_resolved(m_pending_or_error.value());
return on_resolved(m_pending_or_error->value());
return {};
}
void cancel(Error error)
@ -56,7 +58,7 @@ public:
RefPtr<Promise<T>> new_promise = Promise<T>::construct();
on_resolved = [new_promise, func](Result& result) {
auto t = func(result);
new_promise->resolve(move(t));
return new_promise->resolve(move(t));
};
return new_promise;
}