JSFiddle - React, Tailwind, and code Playground

by LyndseyB

HTML

Your name is: <div id="el"></div>

JavaScript

var data = {};
var el = document.getElementById('el');

function ajaxCallback() {
	el.innerHTML = data.name;
}

$.ajax({
	url: 'someurl',
  success: function() {
   data.name = 'Lyndsey';
   
   //ajaxCallback(); 
   // call it here, and data.name has been set already so it will work as expected. Also, this is the success event, which only runs when the AJAX request has had a status 200.
   
  }
});

ajaxCallback(); // runs syncrhonously so will fire as soon as AJAX is called, but the AJAX may not necessarily be complete at this point, which means data.name is still undefined