<div id=test>???</div>
<div id=status>Script is running ...</div>
CSS
#test { font-size: xx-large; color: red; }
JavaScript
// function that makes a request and returns a promise:
function testAjax() {
return $.ajax({
url: '/echo/json/',
type: 'POST',
data: {
json: '123',
delay: 2
}
});
}
// function that expects a promise as an argument:
function displayData(x) {
x.success(function(realData) {
$('#test').text(realData).css({color: 'green'});
});
}
// get a promise from testAjax:
var promise = testAjax();
alert( $.fn.jquery ); //jquery version 1.5.2
// give a promise to other function:
displayData(promise);
// Reached the end of the script:
$('#status')
.text('Main script is done without waiting for data.');
// Example by http://stackoverflow.com/users/613198/rsp
// for http://stackoverflow.com/questions/5316697
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.