gpt4 book ai didi

promise - 如何从 promise 中退出 promise ?

转载 作者:行者123 更新时间:2023-12-04 07:32:50 30 4
gpt4 key购买 nike

如何从 promise 中退出 promise ? perl6 文档没有提供简单的方法。例如:

my $x = start {
loop { # loop forever until "quit" is seen
my $y = prompt("Say something: ");
if $y ~~ / quit / {
# I want to exit the promise from here;
# "break" and "this.break" are not defined;
# "return" does not break the promise;
# I do NOT want an error exception when exiting a promise;
# I want to return a value as the result of this promise;
}
else { say $y; }
}
}

我不想永远处于 promise 循环中。 break()this.break()不被识别,和 return不违背 promise 。

最佳答案

使用 last keyword退出循环。
start 的保留值block 是其最后一条语句返回的值。

所以:

my $x = start {
loop { # loop forever until "quit" is seen
my $y = prompt("Say something: ");
if $y ~~ / quit / {
last
}
else { say $y; }
}
42 # <-- The promise will be kept with value `42`
}

关于promise - 如何从 promise 中退出 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45789784/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com