0

My code is below-:

function get_btc(address) {
    address_transaction(address, user_id, coin_key, deposite_txn_fee, function (callback) {
        for (var j = 0; j < callback.response.data.txs.length; j++) {
            let user_id = callback.user_id;
            //some code//
        }
    });
}

get_label_info(function (err, data) {
    var promise = [];
    for (var i = 0; i < data.length; i++) {
        let address = data[i].address;
        var test_function = function (done) {
            deposite_model.get_coin_info(function (err, data1) {
                var coin_name = data1[0].coin_code;
                const return_functions = get_switch(coin_name);
                if (return_functions) {
                    obj[return_functions](address);
                }
                done(err, data1);
            });
        };
        promise.push(test_function);
    }
    sample();
});

function sample() {
    console.log('all functions has been completed');
}

By the help of above mentioned code i want to excecute all_completed loop when all functions has been completly done. At the initial start get_label_info function is excuted then controller go on to get_btc function.

Please help me how could i run all_completed functions after all functions completed run.

4
  • you are only pushing a single function into the promise array, as the .push is outside of the loop. Commented Mar 20, 2018 at 5:35
  • note: double spaced code with poor indentation doesn't help Commented Mar 20, 2018 at 5:35
  • @Noface - no it isn't - it's just lazy indentation Commented Mar 20, 2018 at 5:36
  • then controller go on to get_btc function - no, you don't call get_btc anywhere in your code Commented Mar 20, 2018 at 5:38

1 Answer 1

1

Try this,

Define test_function outside for loop, It's not the good approach to declare inside the loop.

var test_function = function (address) {
    return new Promise((resolve, reject) => {
        deposite_model.get_coin_info(function (err, data1) {
            if (err)
                return reject(err);
            var coin_name = data1[0].coin_code;
            const return_functions = get_switch(coin_name);
            if (return_functions) {
                obj[return_functions](address);
            }
            return resolve(data1);
        })
    })
}

get_label_info(function (err, data) {
    var promises = [];
    for (var i = 0; i < data.length; i++) {
        promises.push(test_function(address));
    }

    Promise.all(promises).then((data) => {
        sample();
    })
});


function sample() {
    console.log('all functions has been completed');
}
Sign up to request clarification or add additional context in comments.

9 Comments

its works but what happen when controll go into get_btc function
It depends on get_btc content If there is no async call inside that no issue.
var obj = { get_btc: function get_btc(address) { return new Promise((resolve2,reject2) =>{ address_transaction(address,function(callback){ for(var j=0; j<callback.response.data.txs.length;j++) { let data_address=callback.address; cron_model.is_address_exist(address,function(err,data){ if(err){ throw err; } else{ console.log('Alerady exist'); } return resolve2(data); }) } }); }); }, }
var test_function = function(address,user_id,coin_key){ return new Promise((resolve,reject) =>{ deposite_model.get_coin_info(user_id,coin_key,function(err,data1){ if(err){ return reject(err); } var promises1=[]; var coin_name=data1[0].coin_code; const return_functions = get_switch(coin_name); if (return_functions) { promises1.push(obj[return_functions](address)); }; Promise.all(promises1).then((data)=>{ resolve(data1); }); }) }) }
cron_model.get_label_info(function(err,data){ if(err){ throw err; }else{ var promises=[]; for (var i=0;i<data.length; i++) { let address=data[i].address; let user_id=data[i].user_id; let coin_key=data[i].coin_key; promises.push(test_function(address,user_id,coin_key)); } Promise.all(promises).then((data)=>{ sample(); }); } }); function sample() { console.log('all functions has been completed'); }
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.