I have a function in a web application that makes an AJAX call to update an element. I have other functions which use this function, and other functions which use those functions.
Say I have function A which makes an AJAX call. Function B calls function A, and function C calls function B.
Now imagine I have function D, which is another function that makes an AJAX call.
I want to call function C, then function D. The problem is I run into an issue where sometimes function D is using the response that was returned from the function C call. So I don’t want function D to run until a response is received from function C’s AJAX request.
Since the AJAX request happens so far down the chain (C > B > A), it seems like such a hack to have function C accept a callback, then pass that call back to B, which passes that callback to A so that A can call that callback function after it receives the response to it’s AJAX request.
Is this actually the best way to do it, or can it be done more cleanly? If it makes a difference, I’d prefer a solution that doesn’t use any extra libraries (like jQuery for example.)
Thanks a lot for any help.
You want to use promise pattern.