1

I have a small question.I try to run this code and why 5 is before "meow".I thought that reason is that speed of save Db is slower and try to add many operations after console.log(5),but "meow" was last too.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var Cat = mongoose.model('Cat', { name: String });

var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log('meow');
  }
});

console.log(5);
  • 2
    Possible dupe http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – chridam Jun 22 '16 at 10:07

1 Answers1

2

Yes the database operations take time and node js works on the principle of asynchronization so until the operations are finished it proceeds with other task possible. May be the operations you tried after console.log(5) do not take much time.

Rishabh Saxena
  • 274
  • 2
  • 6