Filters on custom script for child table not working- need to click twice

Hi,

I have a custom script, which fetches the stock qty and the warehouse code from api.py file, in the descending order of the stock value, and displays it in the filter for a link field. Though, the script is working, I have to click twice on the field to make it show the right warehouse codes. Is there any way I could improve the code, to make the filters work in just 1 click? Is there anything that I might be missing?

frappe.ui.form.on("Stock Entry", "refresh", function(frm) {
   cur_frm.set_query("s_warehouse", "items",  function (frm, cdt, cdn) 
   {
	var d= locals[cdt][cdn];
	console.log("Item Code:: "+d.item_code);
		frappe.call({
        		method: "epapp.api.get_whse_list_stock",
	        	args: {
        	   		"item_code": d.item_code,
       			},
    		callback: function(r)
		  { 
			console.log("Length"+ r.message.length);
			whse_list = [];
			console.log("whse_list :" + whse_list);
			for(var i = 0; i < r.message.length; i++)
			{
				console.log("r.message::"+r.message[i].warehouse);
				whse_list.push(r.message[i].warehouse);
				console.log("whse_list"+ whse_list);
			}
	 	  }
	    	});

	return { 
		"filters":	[
				["Warehouse","name", "in", whse_list]
				] 
			}

	refresh_field("s_warehouse");
	refresh_field("items");
	});

});

This is my python code:

@frappe.whitelist()
def get_whse_list_stock(item_code):
	records = frappe.db.sql("""select warehouse, sum(ledger.actual_qty) as stock_qty
		from `tabBin` ledger where ledger.item_code = %s GROUP BY ledger.warehouse HAVING stock_qty > 0 ORDER BY stock_qty desc""", item_code, as_dict=1)
	if records:
		return records
	else:
		return