【问题标题】:Resolving nodejs circular dependancies解决node js循环依赖
【发布时间】:2019-11-06 04:04:57
【问题描述】:

我知道这个问题已经被问过好几次了,但我无法在我的项目结构中解决它。

我有 3 个文件:

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

new_order.js 负责初始化值并将其传递给binance.js 以执行订单。

binance.js 然后执行订单并让 websocket 始终运行以等待订单被执行的事件。因为我无法在 websockets 中返回值,所以我在订单完成后立即调用advance.js

advance.js 具有止损/止盈等高级功能。我遇到的问题是,一旦价格达到止损/止盈位,我必须再次致电binance.js 执行卖单。

我的流程是new_order.js -> binance.js advance.js .. 我怎样才能克服这个问题,也有可能从binance.js 返回一个值回到new_order.js 全职正在运行 websocket?

【问题讨论】:

    标签: javascript node.js websocket binance


    【解决方案1】:

    创建一个文件 index.js 并按顺序导入其中的所有内容。然后在所有其余文件中从 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"
    

    【讨论】:

    • 我不确定我做错了什么,但我收到了SyntaxError: Unexpected token import
    • 能否在nodejs中使用原生导入?
    • 基本思路是从index.js中的所有文件导入,然后从其他文件中的index.js导入。
    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2013-02-04
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多