How to use the wait_async function from bindings

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

bindings.wait_async is a function provided by the FFI bindings library that blocks execution of the current thread until an asynchronous function call completes.

812
813
814
815
816
817
818
819
820
821
it('throws if dev_hnd is not an open device handle', () => {
	(() => rtlsdr.wait_async({}, (() => {}))).should.throw();
});

it('throws if listener is not a function', () => {
	(() => rtlsdr.wait_async({}, 'hi mom')).should.throw(TypeError);
});

it('emits an error if rtlsdr_wait_async errors right away', (done) => {
	rtlsdr.mock_set_rtlsdr_dev_contents(dev, 'buffer_ready', true);
fork icon0
star icon1
watch icon0

+ 95 other calls in file

How does bindings.wait_async work?

bindings.wait_async is a function provided by the FFI bindings library that blocks execution of the current thread until an asynchronous function call completes. The function takes in two arguments: a pointer to an opaque "completion object" returned by the asynchronous function, and an optional timeout value in milliseconds. When the function is called, it sets up a loop that repeatedly polls the completion object to determine whether the asynchronous function has completed. If the function has not completed within the specified timeout period (if any), the function throws an error indicating a timeout has occurred. If the function has completed, the function returns the completion object, which can be used to retrieve the result of the asynchronous function call. Overall, bindings.wait_async provides a way to block execution of the current thread until an asynchronous function has completed, which can be useful in cases where synchronous behavior is required or the result of the asynchronous function needs to be immediately available.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { FFI } = require("bindings")("example");

const completionObject = FFI.doSomethingAsync((error, result) => {
  if (error) {
    console.error(error);
  } else {
    console.log(result);
  }
});

const timeout = 5000;
const result = bindings.wait_async(completionObject, timeout);

console.log(result);

In this example, we use bindings.wait_async to block execution of the current thread until an asynchronous function call, FFI.doSomethingAsync, completes. The doSomethingAsync function takes in a callback function that will be called with the result of the asynchronous operation once it completes. We call doSomethingAsync and store the resulting completion object in a variable, completionObject. We then pass completionObject and a timeout value of 5000 milliseconds to bindings.wait_async, which blocks execution of the current thread until the asynchronous function call completes or the timeout period expires. Once the function returns, we retrieve the result of the asynchronous function call from the completion object and log it to the console. Note that in order to use bindings.wait_async, you need to have the FFI bindings library installed and imported in your application.

Other functions in bindings

Sorted by popularity

function icon

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