Skip to content

[Feature] Pass cancellation token to nodeJS module execution function #196

@aim-sealed

Description

@aim-sealed

It is possible to pass cancellation token to .NET invocation and the request to nodeJS can be cancelled.
However, in the same time nodeJS will still execute JS code for the .NET request which have been already cancelled.

So it will be nice to have cancellation token as argument in JS module execution function so JS code can be also cancelled.

The solution example is below.
It is possible to create simple JS object like below to handle cancellation tokens in nodeJS:

type CancellationToken
{
   isCancellationRequested: boolean;
   signal: AbortSignal; // can be used to cancel requests from module execution function 
}

Initialize the object on request:

const abortController = new AbortController();
const cancellationToken : CancellationToken = { isCancellationRequested: false, signal: abortController.signal };

Subscribe on request cancellation:

req.connection.on('close', function (){
   cancellationToken.isCancellationRequested = true;
   abortController.abort();
});

Then this cancellation token can be checked inside module execution function to skip/stop async js code execution.

fetch(url, { signal: cancellationToken.signal }).then(...);

...

if (cancellationToken.isCancellationRequested)
   return;

Feel free to change the provided above solution. This is just example what I expect to have )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions