Skip to Content
avatar image
Former Member

How to execute code in sapui5 after completion of particular function or particular piece of code

Hi All,

When I click on button I will open popup with tree table, to load all Parents and child till 2nd level which is sending 10 odata calls including 5 count calls and 5 data fetching calls.so once below code executed I will start selecting using other code but which is executing before completion of below code.

oModel.attachRequestCompleted(function(oEvent){
 var oTable = sap.ui.getCore().byId("treeTable");
 oTable.expandToLevel(2);
		});

How can I execute this peice of code after completion of above code

if(CONSTANTS.brands && CONSTANTS.brands.length){
	    	var select = CONSTANTS.brands[0].Brand.split(",");
         oTable.removeSelectionInterval(0,oTable.getRows().length-1);
		    for(i=0;i<select.length;i++){
		    	oTable.addSelectionInterval(select[i],select[i]);
		    	console.log("outside");	    	
		    }
	    }
	    else {
	   oTable.removeSelectionInterval(0,oTable.getRows().length-1);
	    }	
	};

Once all the rows are loaded in tree I have to select nodes, as my code is executing before loading 2nd level child's I am unable to select child's of level 2.

Could anyone please help me how to execute code after completion of above code

Thanks,

Best Regards,

Viswanath

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Feb 22, 2017 at 06:17 AM

    try this.

       mainFunction: function(){
          this.function1();
          setTimeout(this.function2, 10);
       },
       function1: function(){
        alert("first function called.");
       },
       function2: function(){
        alert("first function completed!! And second function called after 10 ms.");
       }

    using setTimeout function you can achieve this. function2 will be called after completion of function1 plus 10 ms.

    to test this example just increase time from 10 to 5000 ms and check, once you click OK from first alert, after 5 second only second alert will come.

    hope this help.

    Thanks,

    Akhilesh

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 16, 2017 at 03:37 PM

    Hello Viswanath,

    You can use the the concept of Javascript promise . Refer to the MDN . You can create a promise and resolve them at the end of the code you executed.

    Thanks and Regards,

    Veera

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Veera,

      Thanks for your response.

      could you please share any sample code, that would be helpful.

      Thanks,

      Best Regards,

      Viswanath

  • Feb 21, 2017 at 06:38 PM

    Use the event "toggleOpenState" for the table which indicates if the node is expanded or not.

    init: function(){
        var oTable = sap.ui.getCore().byId("treeTable");
        oTable.attachToggleOpenState(function(oEvent){
              var bExpanded = oEvent.getParameter("expanded");
              if(bExpanded){
                  //Execute any code you want here
             }
        });
    }

    This is the same concept as that of "promise" mentioned by Veera

    Add comment
    10|10000 characters needed characters exceeded