Sleep 
IntuitionApproachExplanation of problem in simple words:1.Sleep Function takes a positive integer millis and sleep function is asynchronous function
2. Sleep fu
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach

# Explanation of problem in simple words:
1.Sleep Function takes a positive integer millis and sleep function is asynchronous function
2. Sleep function returns a promise that should get resolved only after given millis milliseconds

# Doubt: Why not to create Promise inside setTimeout
The standard way for solving this problem is creating a Promise and using setTimeout inside it.
But why cannot we create a setTimeout first and add the Promise inside it?
Well here is the detailed explaination for above doubt
Detailed explanation of above doubt:
setTimeout(() => { return Promise.resolve() }, millis);

setTimeout schedules a task but doesn't return any value that you can await or .then() on. It doesn't return a promise. Its just a async function that triggers after specific time. So function sleep doesn't return any Promise immediatelty.
Although the Promise is created inside the setTimeout, but it doesn't return the Promise to calling code.So there is nothing to do .then() on
# What does setTimeout() returns ?
setTimeout returns a timeout ID (not a Promise)
setTimeout actually returns a timeout ID(a number) which can be useful to cancel the timeout later if needed like clearTimeout(timeoutID).
Point to remember that it doesn't return the result or output of the callback function.
You cannot use this ID to await the result of setTimeout or to know when the callback inside has finished
# Correct Approach:
Creating a Promise and using setTimeout inside it
In this case by creating a Promise we ensure that we are returning a Promise from a sleep function and we can also use .then() on it
So it will create a Promise and resolve after given time is over and also return it from sleep function

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
async function sleep(millis) {
    let ans = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(millis);
        }, millis);
    })

    return ans;
}
 let t = Date.now()
 sleep(100).then(() => console.log(Date.now() - t)) // 100
```
['JavaScript']