Node repl with async await

前端 未结 3 2079
夕颜
夕颜 2021-02-07 00:36

I would like to add support to async/await to node repl

Following this issue: https://github.com/nodejs/node/issues/8382

I\'ve tried to use this one https://gith

相关标签:
3条回答
  • 2021-02-07 01:04

    There is the project https://github.com/ef4/async-repl:

    $ async-repl
    async> 1 + 2
    3
    async> 1 + await new Promise(r => setTimeout(() => r(2), 1000))
    3
    async> let x = 1 + await new Promise(r => setTimeout(() => r(2), 1000))
    undefined
    async> x
    3
    async>
    

    Another option, slightly onerous to start but with a great UI, is to use the Chrome Devtools:

    $ node --inspect -r esm
    Debugger listening on ws://127.0.0.1:9229/b4fb341e-da9d-4276-986a-46bb81bdd989
    For help see https://nodejs.org/en/docs/inspector
    > Debugger attached.
    

    (I am using the esm package here to allow Node to parse import statements.)

    Then you go to chrome://inspect in Chrome and you will be able to connect to the node instance. Chrome Devtools has top-level await, great tab-completion etc.

    0 讨论(0)
  • 2021-02-07 01:12

    As of node ^10, you can use the following flag when starting the repl:

    node --experimental-repl-await
    $ await myPromise()
    
    0 讨论(0)
  • 2021-02-07 01:23

    The idea is to preprocess the command and wrap it in a async function if there is an await syntax outside async function

    this https://gist.github.com/princejwesley/a66d514d86ea174270210561c44b71ba is the final solution

    0 讨论(0)
提交回复
热议问题