How to exit a promise from within a promise? - promise

How do I exit a promise from within a promise? The perl6 docs do not provide an easy method. For example:
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; }
}
}
I do not want to be in the promise loop forever. break() and this.break() are not recognized, and return does not break the promise.

Use the last keyword to quit the loop.
The kept value of a start block is the value returned by its last statement.
So:
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`
}

Related

Puppet test defined variable with an empty string

I'm trying to test if a variable has a value with a simple if in puppet, but it never reachs the else condition that I'm trying to achieve.
ex :
if $::some_var['some:name'] != undef {
$var = $::some_var['some:name']
}
else {
$var = $::some_var['another:name']
}
some_var cames from a json file, and if the ::some_var['some:name'] is emtpy it will put $var as empty, but what I'm trying to achieve is, if the var is defined but is empty to actually go through the else, can this be achieved using for instance
if defined($::some_var['some:name']) {
$var = $::some_var['some:name']
}
else {
$var = $::some_var['another:name']
}
I resolved it by using
$var = ''
if $var =~ String[1] {
notify{"The value is: ${var}": }
}
else {
notify{"The value is: empty": }
}
file {'/tmp/helloworld.txt': ensure => present, content => "Hello World!", }
If I define $var whit some value, I'll met the if condition, if $var is empty I'll met the else.

In the following code variable 'checkNumber' is not incrementing to 1 even after 'if' block get executed and so that Break is not working

In the following code variable 'checkNumber' is not incrementing to 1 even after 'if' block get executed and so that Break is not working where i need to break the loop
var checkNumber =0
for (let i = 0; i < totalRowCountAllocPrj; i++){
allocationObjects.getAllocationStatusfromGrid(i).then(text => {
appAllocStatus = Cypress.$(text).text()
cy.log("Allocation Status :" + appAllocStatus)
if(appAllocStatus == userData.approvalReservedAllocStatus){
allocationObjects.getAppAllCheckBoxesfromGrid(i).click()
checkNumber=checkNumber+1
cy.log("index="+i)
}
else{
cy.log("Project is not Reserved")
}
})
cy.log("number="+checkNumber)
if(checkNumber==1)
{
break
}
The variable checkNumber gets incremented inside an asynchronous command, but the loop is running synchronously.
You can't use break but you should be able to stop executing the commands with the inverse check.
Since checkNumber never should go above 0, it's more sensible to use a boolean
let found = 0
for (let i = 0; i < totalRowCountAllocPrj; i++) {
cy.then(() => {
if (!found) {
allocationObjects.getAllocationStatusfromGrid(i).then(text => {
appAllocStatus = Cypress.$(text).text()
if (appAllocStatus === userData.approvalReservedAllocStatus) {
allocationObjects.getAppAllCheckBoxesfromGrid(i).click()
found = true
}
})
}
})
}
BTW you should be able to rewrite allocationObjects.getAllocationStatusfromGrid() to directly search for userData.approvalReservedAllocStatus and get rid of the loop altogether.

Bash - exit does not exit script (only subshell) [duplicate]

How would you exit out of a function if a condition is true without killing the whole script, just return back to before you called the function.
Example
# Start script
Do scripty stuff here
Ok now lets call FUNCT
FUNCT
Here is A to come back to
function FUNCT {
if [ blah is false ]; then
exit the function and go up to A
else
keep running the function
fi
}
Use:
return [n]
From help return
return: return [n]
Return from a shell function.
Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the
last command executed within the function or script.
Exit Status:
Returns N, or failure if the shell is not executing a function or script.
Use return operator:
function FUNCT {
if [ blah is false ]; then
return 1 # or return 0, or even you can omit the argument.
else
keep running the function
fi
}
If you want to return from an outer function with an error without exiting you can use this trick:
do-something-complex() {
# Using `return` here would only return from `fail`, not from `do-something-complex`.
# Using `exit` would close the entire shell.
# So we (ab)use a different feature. :)
fail() { : "${__fail_fast:?$1}"; }
nested-func() {
try-this || fail "This didn't work"
try-that || fail "That didn't work"
}
nested-func
}
Trying it out:
$ do-something-complex
try-this: command not found
bash: __fail_fast: This didn't work
This has the added benefit/drawback that you can optionally turn off this feature: __fail_fast=x do-something-complex.
Note that this causes the outermost function to return 1.
My use case is to run the function unless it's already running. I'm doing
mkdir /tmp/nice_exit || return 0
And then at the end of the function
rm -rf /tmp/nice_exit

Converting if-else to try-catch within PS

I have the following PS command which is as follows:
if(get-customcmdlet) {
$var = var1
} else {
$var = var2
}
Here get-customcmdlet is throwing an exception.
So, I have modified the above statement as follows:
try {
get-customcmdlet
$var = var1
}
catch {
$var = var2
}
Please let me know if it is correct way to handle the exception generated from get-customcmdlet
You have to understand that try-catch does something different than your earlier if-else.
Try-Catch checks if a execption is thrown, while if(get-customcmdlet) checks if your function returns something unequal $false or $null.
Given that your function supplies something even when an exception occurs on one point, you can just combine both toghether like this:
try {
if(get-customcmdlet) {
$var = var1
}
else {
$var = var2
}
}
catch {
$var = var2
}
This way $var equals var2 when the function returns an exception and when it returns nothing or $false.

RSpec Stubbing: return in a sequence

I know the following things work:
returning a parameter
subject.should_receive(:get_user_choice){ |choices| choices.to_a[0] }
and a sequence (it will return a 0 on the first call, and the second time "exit")
subject.should_receive(:get_user_choice).and_return(0, "exit")
But how to combine them?
what if I would like to return the parameter the first time and then return "exit"
Alternatively:
subject.should_receive(:get_user_choice).ordered.and_return { |choices| choices.to_a[0] }
subject.should_receive(:get_user_choice).ordered.and_return { "exit" }
Not most elegant, but how about:
n = 0
subject.should_receive(:get_user_choice){|choices|
(n += 1) < 2 ? choices.to_a[0] : "exit"
}

Resources