What do with Async in node js : I have problem with async.
Result of this code from pic should be:

First:0

Second:2

Item ADD!

First:1

Second:2

Item ADD!

First:2

Second:2

Item ADD!


And here script should stop.

I know that code is to long but I can't put less.

    connection.query('SELECT `keys`.*,`transaction`.*,`keys`.`id` as kid, `transaction`.`id` as tid  FROM `transaction` JOIN `keys` ON `keys`.`id` = `transaction`.`keys_id` WHERE `transaction`.`status_pay`= 1 and `transaction`.`status` = 1').then(function(rows){
    				    rows.forEach(function(record) {
    				    	var offer = manager.createOffer('76512333220');  
    				    	inventory.forEach(function(item) {
    					    	connection.query('SELECT amount_two FROM transaction where id = \'' + record.tid + '\'').then(function(w){
    							    console.log("First: " + w[0].amount_two);
    							    console.log("Second: " + record.amount);
    							    if(w[0].amount_two <= record.amount)
    								{
    									if(item.market_hash_name == record.real_name)
    									{
    										var asid = item.assetid;
    										connection.query('SELECT count(id) as wynik FROM used where asset_id = \'' + asid + '\'').then(function(wiersze){
    										    if (wiersze[0].wynik == 0) 
    											{
    												var employee = {
    								                        asset_id: asid,
    								                        trans_id: record.tid
    								                    };
    								                 connection.query('INSERT INTO used SET ?', employee).then(function(rows){
    												    offer.addMyItem(item);
    												    console.log('Item ADD!');
    												    connection.query('UPDATE `transaction` SET `amount_two`= `amount_two` + 1 WHERE `id`=\''+record.tid+'\'').then(function(rows){
    													    
    													});
    												});   
    
    											}	
    										});
    									}	
    								}			
    
    							});
    						});
    				    });
    				});


[![enter image description here][1]][1]


  [1]: http://i.stack.imgur.com/WcilH.png