0

I want to run 2 functions one after the other continuously i.e

function a();
function b();
function a();

However function b has to run a total of 20 times before a is called. My code looks like this:

var scaleTimes = 20;



banking();
Array.from({length: scaleTimes}, () => actions());



function actions () {

console.log('Starting');

robot.moveMouse(1246,465);

robot.moveMouse(1280,472);

robot.mouseClick("left", 1);

robot.moveMouse(1246,465);

robot.mouseClick("left",1);

console.log('Done...');

};

function banking () {

console.log('banking...');

robot.moveMouse(842,123);
sleep(600);
robot.mouseClick();
sleep(600);

robot.moveMouse(1024, 336);
sleep(600);
robot.mouseClick();
sleep(600);

robot.moveMouse(709,159);
sleep(600);
robot.mouseClick();
sleep(600);

robot.moveMouse(1064,49);
sleep(600);
robot.mouseClick();
sleep(4000);

console.log('done...');
};

How would i get this done by calling one single function at the start of my code?

lukesudom
  • 17
  • 3
  • It doesn't look like you're using `sleep` right. https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep – evolutionxbox Sep 07 '22 at 10:44

1 Answers1

0

You can do using Promises.

let a = new Promise(function(Resolve, Reject) {
      //
      // your function or logic
      // when completed return Resolve()
      //
  Resolve("done"); // when successful
  Reject("error");  // when error
});

let b = new Promise(function(Resolve, Reject) {
      //
      // your function or logic
      // when completed return Resolve()
      //
  Resolve("done"); // when successful
  Reject("error");  // when error
});


a.then(
  function(value) {
      /* code if it is successful */ 
      b.then(){
          function(value) {
          // again when it is successul then call other function
          }
          function(error) { /* some error */ }
      }
  },
  function(error) { /* some error */ }
);