I am struggling to find a nice logical way of representing this pseudocode in javascript with async calls:
This is controller pseudo code:
// get the data for the main page, server will return login details if not logged in model.getdata(mypage); // keep trying to get a login until the user successfully logs in while (model.loggedin() === false) { // show the login form view.showloginform(model.data); // and push that login to the server model.trylogin(view.login.userdata); if (!model.loggedin()) // show the error to the user and they will click ok view.showloginerrordetails(model.loginresponse); else // try to get the main page data again model.getdata(mypage); } // now we are logged in so we can show the main page view.showmainpage(model.data);
model.getdata is async while waiting for the server to respond view.showloginform is async while waiting for the user to fill the form in model.trylogin will be async while waiting for the server. etc
So obviously my loop wouldn't work if any of the calls were async. And they all are
I think its easy with closures when the loop only happens once, but how would you write all this in a function when the loop could run through several iterations ?
This is just a simple example but I want to support more complex workflows using the same ideas.
I'm really interested in how people structure code like this in javascript so the code is readable and elegant etc.