How to use the cancel_async function from bindings

Find comprehensive JavaScript bindings.cancel_async code examples handpicked from public code repositorys.

bindings.cancel_async is a function in the node-addon-api library that cancels an asynchronous function call.

939
940
941
942
943
944
945
946
947
948
949
				(() => rtlsdr.cancel_async({})).should.throw();
			});


			it('throws if rtlsdr_cancel_async errors', () => {
				rtlsdr.mock_set_rtlsdr_dev_contents(dev, 'mock_return_error', -1);
				(() => rtlsdr.cancel_async(dev)).should.throw();
			});
		});
	});
});
fork icon0
star icon1
watch icon0

+ 95 other calls in file

How does bindings.cancel_async work?

bindings.cancel_async is a function in the node-addon-api library that is used to cancel a pending asynchronous function call. It works by taking a napi_async_work object as an argument, which represents a pending asynchronous function call. When called with a napi_async_work object, bindings.cancel_async cancels the asynchronous function call associated with the object. The function does this by setting a flag on the object that indicates that the call has been cancelled. If the function call has not yet started executing, bindings.cancel_async will prevent it from starting. If the function call is already executing, the function will attempt to interrupt it and terminate its execution as soon as possible. Note that bindings.cancel_async can only cancel asynchronous function calls that were started using the napi_create_async_work() function or similar functions in the node-addon-api library. You cannot use this function to cancel other types of asynchronous operations, such as timers or network requests. Also note that cancelling an asynchronous function call can have unintended consequences, especially if the function was in the middle of performing a critical operation. Therefore, you should use this function with caution and only when it is necessary to cancel a function call that cannot be completed for some reason.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const addon = require("bindings")("myaddon");

function myAsyncFunction() {
  return new Promise((resolve, reject) => {
    const work = addon.doSomethingAsync((err, result) => {
      if (err) {
        reject(err);
      } else {
        resolve(result);
      }
    });

    // Cancel the async function call after 5 seconds
    setTimeout(() => {
      addon.cancelAsync(work);
    }, 5000);
  });
}

In this example, we define a function called myAsyncFunction that performs an asynchronous operation using an add-on module called myaddon. The function creates a Promise that wraps the asynchronous operation and returns it. Inside the Promise constructor, the function calls addon.doSomethingAsync() to start the asynchronous operation and store a napi_async_work object representing the operation in the work variable. The function then schedules a call to addon.cancelAsync() using setTimeout() after 5 seconds to cancel the asynchronous operation. If the asynchronous operation completes successfully before it is cancelled, the Promise will resolve with the result. If the operation is cancelled, the Promise will reject with an error indicating that the operation was cancelled. Note that this is just an example, and the actual asynchronous operation and cancellation logic will depend on the specific use case. However, the basic pattern of using bindings.cancel_async() to cancel a pending asynchronous function call is the same.

Other functions in bindings

Sorted by popularity

function icon

bindings.createKey is the most popular function in bindings (8616 examples)