Node.js: How to programmatically determine asynchronous?

前端 未结 3 1241
半阙折子戏
半阙折子戏 2020-12-19 19:06

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 th

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 19:36

    This is not exactly a solution, just a hint on cases which might be indeed a solution.

    Many franeworks or libraries, define functions which can work either synchronously or asynchronously, depending on whether the last function argument (the callback) is given.

    For example:

    function dummy_sync_or_async(data, callback)
    {
      if (callback)
      { 
         // call async code
         //..
         callback(result);
      }
      else
      {
        // call sync code
        // ..
        return result;
      } 
    }
    

    Then one can check the number of arguments received (for example if working as a proxy to these function methods) and check these against the function signature (i.e function.length)

    Then one can decide whether the function is called in sync or async mode.

提交回复
热议问题