2

我想支持添加到異步/等待到節點REPL與異步節點REPL伺機

關注這一問題:https://github.com/nodejs/node/issues/8382

我試圖用這個https://github.com/paulserraino/babel-repl但它缺少異步等待suppport

我想用這個片段

const awaitMatcher = /^(?:\s*(?:(?:let|var|const)\s)?\s*([^=]+)=\s*|^\s*)(await\s[\s\S]*)/; 
const asyncWrapper = (code, binder) => { 
    let assign = binder ? `root.${binder} = ` : ''; 
    return `(function(){ async function _wrap() { return ${assign}${code} } return _wrap();})()`; 
}; 

// match & transform 
const match = input.match(awaitMatcher); 
if(match) { 
    input = `${asyncWrapper(match[2], match[1])}`; 
} 

我怎麼能這個片段添加到節點REPL定製的eval?

實施例中節點REPL:

> const user = await User.findOne(); 
+0

如何在'--harmony'中使用普通的節點7 REPL? – estus

+0

node -harmony-async-await在節點7 REPL不起作用 –

+0

嗯,應該。我會建議檢查節點版本。顯然,'await'應該在'async'函數內。 – estus

回答

1

的想法是預處理命令,敷在一個異步函數如果 存在AWAIT語法外異步函數

https://gist.github.com/princejwesley/a66d514d86ea174270210561c44b71ba是最終解

+0

我們應該期待一個承諾還是結果? REPL不會掛起,直到它得到你需要暫停並恢復它的結果,但afaik。這是不支持的,至少'repl.pause()'由我退出。另一個問題在這裏:https://github.com/nodejs/node/issues/13209 – inf3rno