Evaluate delayed and ordered expressions using Promise - Javascript Language Basics

Javascript examples for Language Basics:Promise

Description

Evaluate delayed and ordered expressions using Promise

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){// w w w  . j  a  v  a  2 s.  c  o  m
var i = 0;
function run (delay) {
   return function () {
     return new Promise(function (resolve, reject) {
      setTimeout(function () {
        console.log(i++);
        resolve();
      }, delay);
    });
  };
}
Promise.resolve()
   .then(run(500))
  .then(run(1000))
  .then(run(1500));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials