Error: wait.for can only be called inside a fiber

464 Views Asked by At

I have 2 scipts almost identical with a cascade of function calls nested in a fiber.

This one (parsing Tx in a blockchain) with three calls works perfectly

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;
}

Then I have to do something similar with the mempool. In this case I don't have blocks, only transactions, so I have only 2 calls and I get this error message:

Error: wait.for can only be called inside a fiber

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;
}

I don't understand what the problem is. Those two scripts have the same structure !

1

There are 1 best solutions below

0
d0gb3r7 On

I think for array functions like map or filter you need to use the wait.parallel extensions, i.e. in your case something like:

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

(Note: I'm just assuming web3.eth is an array; if not, you should probably add a bit more context to your question, or try to boil down the problem to a more generic example).