Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aim-sealed opened this issue Jun 18, 2024 · 0 comments
Open

Comments

@aim-sealed
Copy link

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant