2017-09-26 25 views
0

我有2個基本上與嵌套在光纖中的級聯函數調用相同的scipt。錯誤:wait.for只能在光纖內調用

這其中有三個電話(解析的Tx在blockchain)的作品完美

wait.launchFiber(blockchain) 

function blockchain() { 
    foreach block { 
     parseBlock (blockIndex) 
    } 
} 

function parseBlock(blockIndex) { 
    foreach Tx in block { 
     parseTx(txHash) 
    } 
} 

function parseTx (txHash) { 
    if (txHashInDB(txHash)) { 
     do something 
    } 
} 

function txHashInDB (txHash) { 
    var theTx = wait.forMethod(Tx, 'findOne', {'hash': txHash}); 
    return (theTx) ? true : false; 
} 

然後,我必須做的內存池類似的東西。在這種情況下,我沒有塊,僅成交,所以我只有2個電話,我得到這個錯誤信息:

錯誤:wait.for只能被稱爲纖維內部

wait.launchFiber(watchMempool); 

function watchMempool() { 
    web3.eth.filter('pending', function (error, txHash) { 
     parseTx(txHash); 
    }); 
} 

function parseTx (txHash) { 
    if (txHashInDB(txHash)) { 
     do something 
    } 
} 

function txHashInDB (txHash) { 
    var theTx = wait.forMethod(Tx, 'findOne', {'hash': txHash}); 
    return (theTx) ? true : false; 
} 

我不明白問題是什麼。這兩個腳本具有相同的結構!

+0

恩,除非我在這裏錯過了一些東西,你根本沒有在第二個腳本中啓動光纖(用'wait.launchFiber')? – d0gb3r7

+0

你是對的,我忘了包括光纖的開發,所以代碼應該是:wait.launchFiber(watchMempool); function watchMempool(){web3.eth.filter(...)} 我在我的問題中做了chande。 :-) – jfjobidon

+0

看來,當我調用過濾器的腳本離開纖維工作區... – jfjobidon

回答

0

我認爲陣列功能,如map或者你需要使用wait.parallel extensions,即在你的情況類似filter

function watchMempool() { 
    wait.parallel.filter(web3.eth, parseTx); 
} 

(注:我只是假設web3.eth是一個數組;如果沒有,你應該爲你的問題添加更多的上下文,或者試着將問題歸結爲更通用的例子)。