Skip to content

Instantly share code, notes, and snippets.

@MayaLekova
Created March 6, 2019 13:14
Show Gist options
  • Save MayaLekova/3c1714b7c605de659ac5aaafb0692a2b to your computer and use it in GitHub Desktop.
Save MayaLekova/3c1714b7c605de659ac5aaafb0692a2b to your computer and use it in GitHub Desktop.
Microticks-independent `chain`
async function chain(iterator, cancelAccessorF) {
if (cancelAccessorF) {
cancelAccessorF(e => {
return Promise.reject(e);
});
}
for (const p of iterator) {
await p;
}
}
// The first 4 tests remain the same
test("Cancelling a promise chain from a generator", async assert => {
const {messages, resolvers, promises, promiseGenerator} = setup();
let cancel = () => {};
// Unfortunatelly we can't use the .finally on `chain` itself
// but instead we use try..catch..finally around the cancellation
chain(promiseGenerator(), c => cancel = c);
resolvers[0]();
await promises[0];
try {
await cancel({tutorialId: 2});
} catch (e) {
// Here we get the cancelled tutorial as `e`
console.log('<catch handler> Tutorial:', e);
} finally {
assert.deepEqual(messages, ["Before promise 1", "Before promise 2"]);
assert.end()
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment