You are reading a single comment by @DavidV and its replies. Click here to read the full conversation.
  • This is a super simple example of using promises to order execution of functions asyncronously.

    // Simple promise
    function Kvp(key, value) {
      return new Promise(function(resolve, reject) {
        console.log(key, value);
        resolve();
      });
    }
    
    // Promise which runs every value second
    function RunOn(value) {
      return new Promise(function(resolve, reject) {
        setInterval(function() {
          console.log(new Date().toISOString().substring(11, 19));
          resolve();
        }, value * 1000);
      });
    }
    
    // Promise which runs once after value seconds
    function RunOnce(value) {
      return new Promise(function(resolve, reject) {
        var runOnce = setInterval(() => {
          console.log("RunOnce", value);
          clearInterval(runOnce); //stop function
          resolve();
        }, value * 1000);
      });
    }
    
    RunOn(1);
    Kvp('A', 1).then(function() {
      Kvp('B', 2);
    });
    RunOnce(5).then(() => {
      Kvp("C", 5);
    }).then(() => {
      Kvp('fin', 12);
    });
    
About

Avatar for DavidV @DavidV started