Controlling await

I have this function seriesSteps, during page load this is called, it will execute all steps,
the display is dynamic loading page where I have div to load that page,
I have button to “goTo” what I want if the current step is executed example showLogs(2) , then I want to click the goTo button
and I want to specify what steps I want to execute, example readMessages(3) when readMessages(3) is executed after it is done, I want all the remaining steps will be executed like await showBlockList(3); until go down to doneProcess()

Is this possible ?

async function seriesSteps(){

        await display(2);    
        await showLogs(2);
        await readMessages(2);    

        await display(3);    
        await showLogs(3);
        await readMessages(3);
		await showBlockList(3);

        await display(4);    
        await showLogs(4);
        await readMessages(4);


        await display(5);    
        await showLogs(5);
        await readMessages(5);

        await display(6);    
        await showLogs(6);
        await readMessages(6);
        await doneProcess();

}

$('#goTo').on('click',function(){
     
     await display(nextpage)//ex: 5

});

Are all those functions actually asynchronous? (Do they need to be?)

Hi,

Yes all are async

Well, if you ALWAYS call readMessages after showLogs after display… and want ALL of these steps to be done synchronously, you probably can combine those functions into one big function…

async function seriesSteps(){

        await display(2);    
        await showLogs(2);
        await readMessages(2);    

        await display(3);    
        await showLogs(3);
        await readMessages(3);
		await showBlockList(3);

        await display(4);    
        await showLogs(4);
        await readMessages(4);


        await display(5);    
        await showLogs(5);
        await readMessages(5);

        await display(6);    
        await showLogs(6);
        await readMessages(6);
        await doneProcess();

}

=>

async function seriesSteps(){

        await doAllTheFuncs(2);
        await doAllTheFuncs(3);
        await showBlockList(3);
        await doAllTheFuncs(4);
        await doAllTheFuncs(5);
        await doAllTheFuncs(6);
        await doneProcess();

}
1 Like

Maybe over-engineered, but just playing.

async function defaultTasks(val = 0) {
  await display(val);
  await showLogs(val);
  await readMessages(val);
}


(async function () {
  const taskList = [[2], [3, showBlockList], [4], [5], [6]];

  for await (const [val, ...extraTasks] of taskList) {
    await defaultTasks(val);

    if (extraTasks.length) {
      for await (const task of extraTasks) {
        await task(val);
      }
    }
  }
  await doneProcess();
}());

https://codepen.io/rpg2019/pen/xxeBvQN

1 Like

in any case… can you await everything? yes. does it rather defeat the purpose of asynch functions? also yes

Can you elaborate? How would you improve this. Are you talking as far as the actual design/architecture of the process?

Yeah… it goes back to the original question i had of whether all those functions are actually async or not… I’m not sure how I see that something called “display” or “showLogs” requires asynchronous operations…

the idea behind an asynchronous operation is to not operate synchronously and avoid blocking. To await everything means its all synchronous and blocks.

1 Like