macabeus / js-proposal-algebraic-effects

📐Let there be algebraic effects in JS
177 stars 4 forks source link

Passing parameters to effects #20

Closed brucou closed 4 years ago

brucou commented 4 years ago

Given the syntax perform String with a string identifying the effect to be run how do you pass arguments to the matching effect handler?

macabeus commented 4 years ago

Despite it isn't explicit on readme, it's already possible to write:

function getName(user) {
  const effectName = 'ask_name';
  let name = user.name;
  if (name === null) {
    name = perform effectName; // we are using a variable instead of a literal string
  }

  return name;
}

const arya = { name: null };

try {
  const aryaName = getName(arya);
  console.log('Arya name:', aryaName)
} handle (effect) {
  if (effect === 'ask_name') {
    await wait(1000);
    resume 'Arya Stark';
  }
}

Thank you for open new issues 😄