Created
March 6, 2019 13:14
-
-
Save MayaLekova/3c1714b7c605de659ac5aaafb0692a2b to your computer and use it in GitHub Desktop.
Microticks-independent `chain`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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