Skip to content
Advertisement

How to separate XMLHttpRequest from the main function for better visbility/testibility (without Promises / asnyc/await )

Imagine this function:

JavaScript

This works fine, but it is impossible to test, and this function has become a monster. So I’d like to refactor it, by separating all the different parts in their own unique functions.
The problem is, I do not know how to extract the XHR part and still keep it working.
I cannot use Promises nor asnyc/await and have to stick to using plain XHR.
What I’d normally do is to create a seperate async function for the ajax call (or the xhr in this case). Simply await it’s result and go from there. Easy to separate. But I do not have the luxury of await or anything this time.

What I am trying to get at is something like this

JavaScript

Advertisement

Answer

You can implement a callback-based API:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement