Avatar ·

How to implement a sleep function (delay) in TypeScript?

Hi everyone! I'm a beginner developer in TypeScript and Angular. I've encountered a task: I need to pause code execution, for example, to simulate data loading or add a delay before retrying an API request. In JavaScript, I've seen the setTimeout function, but it doesn't block execution—it just postpones the callback call. I need something like sleep(2000) so that code execution stops for 2 seconds and then continues. Here's what I tried: ```typescript function sleep(ms: number) { setTimeout(() => { // something should happen? }, ms); } async function loadData() { console.log('Starting loading...'); sleep(2000); // I want to wait 2 seconds console.log('Data loaded'); // this executes immediately } ``` But the code doesn't work—