Delay/sleep in Typescript

If you ever wanted to pause the script execution, independent of anything, this script might be useful:

/* *****************************************
   ********** DELAY IN TYPESCRIPT **********
   ***************   USAGE   *************** 
  
  import { delay } from "../utils/helpers";
  public <function_name> = async () => {
    ...
    await delay(5000);
    console.log("Waited 5s");
  }
 */

export const delay = (ms: number) => {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
};

Note that the function in which you want to use delay has to be async function.

Another way is just to use this simple:

browser.pause(500);

Comments