Datatable loading when datasource is function

Hi. How can I populate the datatable with the results of function. For example in this snippet. Code Snippet. Function creates json array and I want display this array in datatable. It seems what function executes after datatable is rendered. But how can I do it before?

function rowsRedraw(){
  const reps=[];
  let nRows = $$("rws").getValue();
  for(let i=1;i<=nRows;i++){
  	reps.push({rw:i,val:"Row number "+i}); 
  $$("tab").define('data', reps);

Thank you. It is working.