This example demonstrates a simple do-while
statement. This function will return aaaaa
.
note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.
export const main = asl.deploy.asStateMachine(async () => {
let counter = "";
do {
counter = `${counter}a`;
} while (counter != "aaaaa");
return counter; //returns "aaaaa"
});
export const main = asl.deploy.asStateMachine(async () => {
let counter = "";
do {
counter = `${counter}a`;
} while (false);
return counter; //returns "1"
});
This example demonstrates a do-while
statement with a break
statement. The do-while statement will break once the container equals aa
, this function will return aa
.
note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.
export const main = asl.deploy.asStateMachine(async () => {
let counter = "";
do {
counter = `${counter}a`;
if (counter == "aa") {
break;
}
} while (counter != "aaaaa");
return counter; //returns "aa"
});
This example demonstrates a do-while
statement with an early return
. The function will return from within the do-while statement, this function will return aa
.
note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.
export const main = asl.deploy.asStateMachine(async () => {
let counter = "";
do {
counter = `${counter}a`;
if (counter == "aa") {
return counter; //returns "aa"
}
} while (counter != "aaaaa");
throw new Error("this should not happen");
});
This example demonstrates a do-while
statement with a continue
statement. The function will not add b
to result in one of the iterations, this function will therefore return bbbb
(4 x b
instead of 5 x b
).
note: as incrementing/decrementing numbers is not supported in ASL the examples uses a string (and string concatenation) as a counter.
export const main = asl.deploy.asStateMachine(async () => {
let counter = "";
let result = "";
do {
counter = `${counter}a`;
if (counter == "aa") {
continue;
}
result = `${result}b`;
} while (counter != "aaaaa");
return result; //returns "bbbb"
});