/**
 * Demonstrates the basics of cancellation scopes.
 */ import { CancelledFailure , CancellationScope , sleep } from '@temporalio/workflow' ; export async function cancelTimer ( ) { try { await CancellationScope . cancellable ( async ( ) => { const promise = sleep ( 1 ) ; CancellationScope . current ( ) . cancel ( ) ; await promise ; } ) ; } catch ( e ) { if ( e instanceof CancelledFailure ) { console . log ( 'Timer cancelled 👍' ) ; } else { throw e ; } } } /**
 * Alternative implementation with cancellation from an outer scope.
 */ export async function cancelTimerAltImpl ( ) { try { const scope = new CancellationScope ( ) ; const promise = scope . run ( ( ) => sleep ( 1 ) ) ; scope . cancel ( ) ; await promise ; } catch ( e ) { if ( e instanceof CancelledFailure ) { console . log ( 'Timer cancelled 👍' ) ; } else { throw e ; } } } /**
 * Demonstrates how to clean up after cancellation.
 */ import { proxyActivities , isCancellation } from '@temporalio/workflow' ; const { httpPostJSON , httpGetJSON , cleanup } = proxyActivities ( { startToCloseTimeout : '10m' , } ) ; export async function handleExternalWorkflowCancellationWhileActivityRunning ( url , data ) { try { await httpPostJSON ( url , data ) ; } catch ( err ) { if ( isCancellation ( err ) ) { console . log ( 'Workflow cancelled' ) ; await CancellationScope . nonCancellable ( ( ) => cleanup ( url ) ) ; } throw err ; } } export async function nonCancellable ( url ) { return CancellationScope . nonCancellable ( ( ) => httpGetJSON ( url ) ) ; } export function multipleActivitiesSingleTimeout ( urls , timeoutMs ) { return CancellationScope . withTimeout ( timeoutMs , ( ) => Promise . all ( urls . map ( ( url ) => httpGetJSON ( url ) ) ) ) ; } /**
 * Demonstrates how to make Workflow aware of cancellation while waiting on nonCancellable scope.
 */ export async function resumeAfterCancellation ( url ) { let result = undefined ; const scope = new CancellationScope ( { cancellable : false } ) ; const promise = scope . run ( ( ) => httpGetJSON ( url ) ) ; try { result = await Promise . race ( [ scope . cancelRequested , promise ] ) ; } catch ( err ) { if ( ! ( err instanceof CancelledFailure ) ) { throw err ; } result = await promise ; } return result ; } /**
 * Demonstrates how to use cancellation scopes with callbacks.
 */ function doSomething ( callback ) { setTimeout ( callback , 10 ) ; } export async function cancellationScopesWithCallbacks ( ) { await new Promise < void > ( ( resolve , reject ) => { doSomething ( resolve ) ; CancellationScope . current ( ) . cancelRequested . catch ( reject ) ; } ) ; } export async function sharedScopes ( ) { const p1 = httpGetJSON ( 'http://url1.ninja' ) ; const p2 = httpGetJSON ( 'http://url2.ninja' ) ; const scopePromise = CancellationScope . cancellable ( async ( ) => { const first = await Promise . race ( [ p1 , p2 ] ) ; CancellationScope . current ( ) . cancel ( ) ; return first ; } ) ; return await scopePromise ; } export async function shieldAwaitedInRootScope ( ) { let p = undefined ; await CancellationScope . nonCancellable ( async ( ) => { p = httpGetJSON ( 'http://example.com' ) ; } ) ; return p ; }