简体   繁体   中英

Best practice for handling async Javascript code in a application framework

I am trying to build a web application, where I have abstracted logic in a Javascript controller class. I want to minimize function calls outside of that class.

Once in a page in my application, whenever a user performs a task, I want the mark a previous action as completed on my server. So I call a method in my Javascript class eg EndTask(id) , which in turn sends out an ajax command to the server. No I want to wait for a response to my ajax command before allowing the user to proceed further.

I also want to ensure that my Javascript API is kept to a minimum and the page do not have to register any callback.

So in essence, I would like to call just EndTask() , and when the function returns, the page would understand that the whole process of marking the task as completed has been performed.

Is there any way to do this in Javascript, without marking the ajax call as synchronous?

It is possible to make an asynchronous function synchronous in JavaScript but to do so you would need to make use of generators which are currently only supported by Firefox.

I would suggest that you synchronous AJAX calls and fall back on generators in Firefox since it doesn't support synchronous XMLHttpRequest objects. Beware however that this might lead to redundancy in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM