few-pizza-8232
02/22/2023, 4:26 AMPromiseTrigger
and FutureTrigger
I have a UI object with callbacks for confirmation and cancellation. The code that sets it up looks like this:
class PromptServer {
public static function prompt() {
final confirmOrCancel = // instantiate UI object
final pt = Promise.trigger();
confirmOrCancel.confirmationHandler = () -> {
trace("confirmed");
pt.trigger(Success("bar"));
};
confirmOrCancel.cancelHandler = () -> {
trace("cancelled");
pt.trigger(Failure(new Error(null, "foo")));
};
return pt.asPromise();
}
}
Another class calls prompt()
to try to pass along the Promise
, like so:
function showPrompt() {
return PromptServer.prompt().next(val -> {
trace(val);
return val;
});
}
The returntype of showPrompt()
is Promise<String>
, but when I trigger the confirmation handler, I only see the "confirmed" trace. When I step into pt.trigger
, the CallbackList
is empty. I've tried this with both Promise.trigger()
and Future.trigger()
to the same effect.
Am I misusing something here, or misunderstanding something about how Future
and Promise
are supposed to function?little-activity-86808
02/22/2023, 1:34 PMlittle-activity-86808
02/22/2023, 3:36 PMpackage;
using tink.CoreApi;
@await class Futures
{
static function main()
{
var inst = new Futures();
}
function new()
{
init();
}
@async function init()
{
var s = @await successOrFailure(true);
trace('Should return a true from Succed(true): $s');
var f = @await successOrFailure(false);
trace('Should return an Error from Failure: $f');
}
function successOrFailure(succeed:Bool)
{
return Future.async(yield -> succeed ? yield(Success(true)) : yield(Failure(new Error(101,'I am failure.'))));
}
}
little-activity-86808
02/22/2023, 3:38 PMcool-psychiatrist-49311
02/22/2023, 9:48 PMcool-psychiatrist-49311
02/22/2023, 9:48 PMcool-psychiatrist-49311
02/22/2023, 9:49 PMlittle-activity-86808
02/23/2023, 7:18 AMcool-psychiatrist-49311
02/23/2023, 7:39 AMcool-psychiatrist-49311
02/23/2023, 7:40 AMcool-psychiatrist-49311
02/23/2023, 7:41 AMlittle-activity-86808
02/23/2023, 7:43 AMlittle-activity-86808
02/23/2023, 7:48 AM@async function init()
{
try
{
var s = @await successOrFailure(true);
trace('Should return a true from Succed(true): $s');
var f = @await successOrFailure(false);
trace('Should return an Error from Failure: $f');
}
catch(e)
{
trace(e);
}
}
function successOrFailure(succeed:Bool):Surprise<Bool, Error>
{
return Future.async(yield -> succeed ? yield(Success(true)) : yield(Failure(throw new Error(101,'I am failure.'))));
}
little-activity-86808
02/23/2023, 7:49 AMlittle-activity-86808
02/23/2023, 7:49 AMlittle-activity-86808
02/23/2023, 7:49 AMlittle-activity-86808
02/23/2023, 7:50 AMfew-pizza-8232
02/24/2023, 4:05 AMhandle
on the other end because I was trying to get the callback to tell me it was doing anything before I even tried!
A bit surprising that it doesn't trigger next
callbacks if there's nothing to handle
the final result, even if those have side effects. But I can understand the rationale.cool-musician-79004
03/09/2023, 11:47 AMWarning : Std.is is deprecated. Use Std.isOfType instead.
elegant-twilight-61392
03/09/2023, 4:16 PMcool-musician-79004
03/09/2023, 6:06 PMelegant-twilight-61392
03/09/2023, 6:08 PMambitious-knife-25690
03/09/2023, 6:37 PMcool-musician-79004
03/09/2023, 8:14 PMcool-psychiatrist-49311
03/13/2023, 1:12 PMcool-musician-79004
03/14/2023, 6:35 AMlittle-oxygen-79174
03/18/2023, 9:49 PMstraight-twilight-37046
03/20/2023, 4:46 AMstraight-twilight-37046
03/20/2023, 4:46 AM