0

I often see this pattern:

request( 'https://www.apple.com/macbook/', function( err, result ) {
    if ( err ) return res.( 500 ).send( "Error" )

    mac = new Macs({
        mac: result
    })

    mac.save()
})

This suggests the if statement can reliably execute before the mac.save(). But trying this in other situations, I'm seeing thing happen before the if statement is considered (or the commands in it executed completely).

So is it blocking? While we're on it, is var blocking?

Noah
  • 4,601
  • 9
  • 39
  • 52

2 Answers2

0

Yes, both var and if are blocking.

The only things in Node.js that are not blocking is anything related to I/O such as disk access, network access & co.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
0

In node.js, every single operation in a block will be executed after the previous one, but sometimes, the result take some time to come (Async function like I/O)

So yes, a "if" is blocking, meaning you won't execute "mac.save()" before it's over.

For a simple example, just try to make an infinite loop. While you do not get out of this loop, you won't do anything else. You won't even be able to execute the callback of an async function, or event.

YlinaGreed
  • 239
  • 1
  • 2