I am using truffle (javascript) to test my solidity contracts and want to automatically run my contract functions a large number of times. I can't figure out how to make a for loop work with promises however as I need it to wait until the whole loop is finished to start again. I'm currently just using a regular for loop with the promises chained up within it. I have included below my roughly anonymised code.


for(var j = 0; j < 5; j++) {
    //create parameters for function
    runFunc(param1 ,param2);

function runFunc(param1, param2) {
    contract.func1.call(param1).then(function(return1) {
        var var1 = = web3.toAscii(account1hash.valueOf());
        contract.func2(param2, var1).then(function() {
            contract.func3(param1, param2);

1 Answer 1


you can do something like this...

NB: accounts is a global variable with test wallets passed in to the test by truffle. should.be.fulfilled is using the chai-as-promised NPM module - checks for errors

  it('Checks we are keeping track of total enlisted', async function() {
    for (var i=0; i < accounts.length; i++) {
      await this.WhitelistV2.enlist(accounts[i], {from: _}).should.be.fulfilled;  
      total +=1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.