Async and await call

Need to call my 2nd function after fetching the details of 1st call function. whats wrong in this code…?

function to fetch Movement details

async get_movement_sheet(frm) {
    if (frm.doc.customer) {
        return frappe.call({
            method: "logistics.utils.get_movements",
            args: {
                'customer': frm.doc.customer, 'fromDate': frm.doc.from_date, 'toDate': frm.doc.to_date
            },
        })
    }
},

fill_goods_movement_sheet: async function (frm) {
    if (frm.doc.customer) {
        var dtl = await frm.events.get_movement_sheet(frm)
        }}

#Function to fetch the item details 
    async get_Item_detail(frm) {
    if (frm.doc.customer) {
        return frappe.call({
            method: "logistics.utils.get_Item",
            args: {
                'customer': frm.doc.customer, 'fromDate': frm.doc.from_date, 'toDate': frm.doc.to_date
            },
        })
    }
},

fill_Item_detail: async function (frm) {
    if (frm.doc.customer) {
        var dtl = await frm.events.get_Item(frm)
        }}

 to_date: async function (frm) {
         frm.trigger("fill_goods_movement_sheet");
         #Need to call the fill_Item_detail function after fetching and full details of fill_goods_movement_sheet
         frm.trigger("fill_Item_detail");
   }

The easiast way is using set async to false and access the data outside the frappe.call

        let branch;
		let current_date;
		let seven_dates;

		frappe.call({
			method: api_url,
			args: {emailid: email},
			async: false,
			callback: function(res) {
				branch = res.message.branch;
				current_date = res.message.current_date;
				seven_dates =  res.message.seven_dates;
			}
		});

console.log (branch);

Another way is use the second frappe call code inside the first frappe. call like below

                frappe.call({
			method: api_url,
			args: {emailid: email},
			callback: function(res) {
                        let branch__id = frm.doc.branch;
                       let branch__name = res.message.branch_name;
                       frm.set_value('branch_id', branch__id);
                       frm.set_value('branch_name', branch__name);
                      frm.set_df_property('branch_name', 'read_only', branch__id);

                     //-----------------------------

				frappe.call({
				method: api_url2,
				args: {branch_param: branch__id},
				callback: function(res_questions) {
					frm.doc.questions = []

					$.each(res_questions.message, function(_i, e){
						let entry = frm.add_child("questions");
						entry.question = e[2];
					});
					refresh_field("questions");
					}
				});

				//-----------------------------

			 	}
            });
5 Likes

Its working as required, Hope this is the recommended method.