resumable thenables

Hi, i'm curious as to the reasons why the "relatively-new" async await try-catch was not spec'd to support this case of resumable thenables:

let thenable = {
  then: (try_block, catch_block) => {
    throw(['ask_name', try_block, catch_block]);
  },
};

try {
  const value = await thenable;
} catch ([value, try_block, catch_block]) {
  if (value === 'ask_name') try_block('Arya Stark');
}

Because promises can only resolve to one value, not multiple times. A try block is not a loop :-)

A simpler example might be

const thenable = {
  then(onFulfill) {
    setTimeout(onFulfill, 100);
    setTimeout(onFulfill, 200);
  },
};

await thenable;
console.log('done');

— how often do you expect "done" to be logged, twice? That kind of control flow would be surprising to most people. A section of code is entered once, and left once1.

1: or never, in case of divergence

Promises aren’t resumable because they don’t do any work. A promise is a placeholder for the future result of work that has likely already been completed. They’re not an appropriate abstraction to use when you want to deal with async work prior to its failure or success.

3 Likes