Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: How to programmatically determine asynchronous?

I'd like to be able to take a function that doesn't take a callback and determine if it will execute asynchronously.

In particular, I'm working with Node.js on the Intel Edison using mraa, and it has native C++ implemented functions like i2c.readReg(address) that doesn't accept a callback.

  1. How can I determine if a function is blocking the processor for other system processes?
  2. How can I determine if other JS can run in the interim?

Or am I not even approaching this the right way?

like image 883
srlm Avatar asked Sep 19 '26 00:09

srlm


1 Answers

You can't really determine asynchronicity programmatically. It should be clear from the API presented because if it's asynchronous, then there pretty much have to be signs of that in the way you use it.

If a function is asynchronous, then that means that it does not directly return the result from the function call because the function returns before the result is ready. As such, the documentation for the function has to tell you how to obtain the result and if it's asynchronous there has to be another mechanism such as:

  1. a callback function you can pass in
  2. a returned promise
  3. some sort of event listener on the object
  4. some other notification mechanism
  5. examine the code of the function
  6. function naming convention (such as the suffix "Sync" that node.js uses)

If the function directly returns the result of the function call, then it is synchronous and other Javascript code will not run during that call.


If a function is not already asynchronous, the only way to turn that into an async operation is to run it in a different thread or process and marshall the value back to the main thread (calling some sort of callback in the main thread when the value is ready).

like image 169
jfriend00 Avatar answered Sep 20 '26 12:09

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!