Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function doSomething() {
  var deferred = jQuery.Deferred();  
  
  // Asynchronous action
  setTimeout(function() {    
    deferred.resolve("some data");
  }, 1000);
  
  return deferred.promise();
}
function doSomethingElse() {
  var deferred = jQuery.Deferred();
  
  // Has to wait for asynchronous action
  doSomething()
    .then(function(data) {
      console.log(data); // Should be "some data"
    
      deferred.resolve(data + " that's been added to");
    })
  ;
  
  return deferred.promise();
}
// Waits for everything to be done that's above, then console.logs end result
doSomethingElse()
  .then(function(data) {
    console.log(data); // Should be "some data that's been added to"
  })
;
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers