Search code examples
javascriptnode.jswebsocketbinance

Resolving nodejs circular dependancies


I know this question has been asked several times but I cant solve it in my project structure.

I have 3 files:

  1. new_order.js
  2. binance.js
  3. advance.js

new_order.js is responsible for initializing values and passing it to binance.js to execute an order.

binance.js then executes a the order and has a websocket running at all times to wait for the event where the order is filled. Because I can't return values in websockets, I call advance.js right after the order has been filled.

advance.js has advance functions like having a stoploss/take profit. The problem I am having is, once the price reaches the stoploss/take profit level, I have to call binance.js again to execute a sell order.

My flow is new_order.js -> binance.js <-> advance.js .. how can I overcome this issue, and also is it possible to return a value from binance.js back to new_order.js from a fulltime running websocket?


Solution

  • Create a file index.js and import everything there in order. Then in all rest of files import from index.js.

    // index.js
    import * from "new_order"
    import * from "binance.js"
    import * from "advance.js"
    
    // binance.js
    import {func_from_advance} from "index.js"
    
    // advance.js
    import {func_from_binance} from "index.js"