Skip to content

Latest commit

 

History

History
198 lines (162 loc) · 8.93 KB

switch.md

File metadata and controls

198 lines (162 loc) · 8.93 KB

simple switch

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const arr = [1, 2, 3];
  let result = "";

  // use a for loop to append all numbers to a single string
  for (const item of arr) {
    switch (item) {
      case 1:
        result = `${result}one`;
        break;
      case 2:
        result = `${result}two`;
        break;
      default:
        result = `${result}three`;
        break;
    }
  }
  return result;
});

switch case falls through

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const arr = [1, 2, 3];
  let result = "";

  // use a for loop to append all numbers to a single string
  for (const item of arr) {
    switch (item) {
      case 1:
      case 2:
        result = `${result}not-three`;
        break;
      default:
        result = `${result}three`;
        break;
    }
  }
  return result;
});

switch case non empty fall through

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const arr = [1, 2, 3];
  let result = "";

  // use a for loop to append all numbers to a single string
  for (const item of arr) {
    switch (item) {
      case 1:
        result = `${result}1`;
      case 2:
        result = `${result}1or2`;
      default:
        result = `${result}1or2or3`;
    }
    result = `${result}|`;
  }
  return result;
});

switch case falls through to default

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const arr = [1, 2, 3];
  let result = "";

  // use a for loop to append all numbers to a single string
  for (const item of arr) {
    switch (item) {
      case 1:
        result = `${result}one`;
        break;
      case 2:
      default:
        result = `${result}not-one`;
        break;
    }
  }
  return result;
});

switch default falls through

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const arr = [1, 2, 3];
  let result = "";

  // use a for loop to append all numbers to a single string
  for (const item of arr) {
    switch (item) {
      default:
      case 1:
        result = `${result}not-three`;
        break;
      case 3:
        result = `${result}three`;
        break;
    }
  }
  return result;
});

switch with block

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const arr = [1, 2, 3];
  let result = "";

  // use a for loop to append all numbers to a single string
  for (const item of arr) {
    switch (item) {
      default:
      case 1: {
        result = `${result}not-three`;
        console.log(result);
        break;
      }
      case 3: {
        result = `${result}three`;
        console.log(result);
        break;
      }
    }
  }
  return result;
});

create aws account

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  const createAccount = await asl
    .sdk(Organizations)
    .createAccount({
      parameters: { AccountName: "test", Email: "[email protected]" },
    });
  let creationStatus: string | undefined = undefined;
  do {
    const describeResult = await asl
      .sdk(Organizations)
      .describeCreateAccountStatus({
        parameters: {
          CreateAccountRequestId: createAccount.CreateAccountStatus!.Id,
        },
      });
    creationStatus = describeResult.CreateAccountStatus?.State;
    switch (creationStatus) {
      case "FAILED":
        throw new Error("account creation failed");
      case "IN_PROGRESS":
        await asl.wait({ seconds: 1 });
    }
  } while (creationStatus !== "SUCCEEDED");

  console.log(createAccount.CreateAccountStatus?.AccountId);
  return createAccount.CreateAccountStatus?.AccountId;
});