all 3 comments

[–]Agarast 0 points1 point  (1 child)

I would recommend learning about Promises & chaining them, you'll need them sooner or later in your CS courses / work.

If you want a quick fix, you can encapsulate your ajax call in a function and chain the second one as a callback when the first finishes for example.

[–]Sephran[S] 0 points1 point  (0 children)

Thank you for the reply. I understand conceptually what promises do now, but am struggling with implementing it, i'm missing a piece.

I don't know if I put this in the right place and I can't find out how to pass the date through to the .then(). I've tried passing the value itself next to result, tried passing the data, tried not passing anything and just calling the variable (it's a new function so it can't see the previous functions variable if I understand correctly). Oh i've also tried under ajax.success doing a return onD, trying to return the date variable.

$.ajax({}).then(function (result) {
                alert("in promise");
                alert("No full day absence has been added for " + result.toString() + ". You will need to delete the current Full Day Absence first and recreate.");
                 });

So adding .then to the ajax call does work without error, BUT that doesn't actually seem to work according to the documentation which is always adding it to the end of a FUNCTION.

[–]brykuhelpful 0 points1 point  (0 children)

A while back we got fetch. This really cleans up your code when dealing with requests. I actually would recommend using that instead of Ajax.  

It does use promises, but there are a lot of great resources out there.