createSubscription

node v14.20.1
version: 1.0.1
endpointsharetweet
const prex = require('prex'); async function createSubscription(token) { // emulate a subscription that can be cancelled const deferred = new prex.Deferred(); const rego = token.register(() => deferred.reject(new prex.CancelError())); try { await deferred.promise; } finally { rego.unregister(); } } async function testSubscription(token) { const tokenSource = new prex.CancellationTokenSource([token]); const s = createSubscription(tokenSource.token); const p = prex.delay(prex.CancellationToken.none, 500); try { await Promise.race([s, p]); } finally { tokenSource.cancel(); tokenSource.close(); } } async function main() { try { await testSubscription(prex.CancellationToken.none); } finally { await prex.delay(prex.CancellationToken.none, 500); } } process.on('unhandledRejection', (reason, promise) => { console.log(`Unhandled Rejection: ${reason}`); }); main().catch(e => console.warn(`caught on main: ${e}`));
Loading…

no comments

    sign in to comment