I am new to Node.js and was trying to understand async and await terms.
In the process, I got to know about Promise.
As I understood, with the help of it, we can make the function non-blocking.
I tried the following code,
function get() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            let out = 0
            const n = 9_900_000
            console.log('start looping')
            for (let i = 0; i < n; i++) {
                out += Math.cos(i)
            }
            console.log('done looping')
            resolve(out)
            console.log('resolved')
        })
    })
}
console.log('before function calling')
r = get()
console.log('After function calling')
r.then(console.log)
console.log('in the end of file')We find that get is behaving like a non-blocking function.
function get() {
    return new Promise((resolve, reject) => {
        let out = 0
        const n = 9_900_000
        console.log('start looping')
        for (let i = 0; i < n; i++) {
            out += Math.cos(i)
        }
        console.log('done looping')
        resolve(out)
        console.log('resolved')
    })
}
console.log('before function calling')
r = get()
console.log('After function calling')
r.then(console.log)
console.log('in the end of file')In other words, I removed the setTimeout.
Now the get becomes a blocking function.
Why is this happening?
 
    