Variables and using it inside frappe.call

Hello;

How I can use a variable inside the frappe.call method?
For example, in the below script, I configured var rows_quantity = 0; and I need to use it inside the frappe.call, but it seems that it is not possible.

The problem now that I need to know the number of rows of the child table, and I am looping within this child table inside the frappe.call, I need to count the child table rows to know the rows quantity, how?

var hops_calculation = function(frm, cdt, cdn) {
	var item = locals[cdt][cdn];
	var rows_quantity = 0;
	frm.set_value('total_price', 0);

frappe.call({
	method: "taxi.taxi.doctype.trip_order.trip_order.get_settings",
	callback: function(r) {
		if (r.message) {
			cur_frm.set_value("discounted_hop_no", r.message[0]);
			$.each(frm.doc.items, function(i, row) {
				if (row.to_metric) {
					if (i < (r.message[0] - 1)) {
						if (flt(row.to_metric) > flt(frm.doc.origin_metric)) {
							row.selected_metric = row.to_metric;
						}
						else {
							row.selected_metric = frm.doc.origin_metric;
						}
					}
					else if (i >= (r.message[1]-1)) {
						if (flt(row.to_metric) >= flt(frm.doc.items[i-1].to_metric))
							row.selected_metric = row.to_metric;
						else
							row.selected_metric = frm.doc.items[i-1].to_metric;
					}
					else {
						if (flt(row.to_metric) > flt(frm.doc.items[i-1].to_metric))
							row.selected_metric = row.to_metric;
						else if (flt(row.to_metric) < flt(frm.doc.items[i-1].to_metric))
							row.selected_metric = frm.doc.items[i-1].to_metric;
						else
							row.selected_metric = r.message[2];
					}
					if (row.ozw == 1)
						row.hop_price = 0;
					else
						row.hop_price = flt(row.selected_metric);
					frm.set_value('total_price', frm.doc.total_price + row.hop_price);
					refresh_field("items");
					rows_quantity = this.rows_quantity + 1;
				}
			})
		}
	}
})
frappe.msgprint(__("Row #{0}", [rows_quantity]));
frm.set_value('final_destination', frm.doc.items[i-3].to);
refresh_field("items");
cur_frm.refresh();
}

Regards
Bilal

Move these functions after your $.each call inside the callback

Yes, that is correct.
But how to use the variables outside the callback function?
Regards
Bilal

You will have to structure your code differently. Javascript works on event based model, and not the usual top down approach you are familiar with.

You can call a function from your callback and do rest of the operations there.