0

My requirement is to firstly open the db database and then execute the second method. I tried I haven't done something like that in JS so any advice/guidance is greatly appreciated.

My code can be found here. How can I make this asynchronous? I have the option of setTimeout which I don't find a good practise...

Any examples will be extremely helpful.

  • 1
    http://stackoverflow.com/questions/30036/javascript-and-threads Some related keywords for search: JavaScript Multi-threading. JavaScript Worker Threads. – Tony Oct 30 '13 at 11:54
  • 1
    JavaScript by itself does not give a method for user-defined functions to be asynchronous - the only way to run asynchronous code is through browser-defined functions such as `XMLHttpRequest`, `setTimeout`, etc. or web workers. Also see [Asynchronous programming in javascript (NOT AJAX)](http://stackoverflow.com/questions/3294281/asynchronous-programming-in-javascript-not-ajax). – Qantas 94 Heavy Oct 30 '13 at 11:54
  • is this browser on node.js? – exebook Oct 30 '13 at 12:01
  • yes I will be working with node.js –  Oct 30 '13 at 12:04
  • I mean the code you posted on pastie.org, is this code executed in Node.js? – exebook Oct 30 '13 at 12:06
  • yes it is executed with node –  Oct 30 '13 at 12:28

1 Answers1

0

This code is from this https://github.com/mongodb/node-mongodb-native README

  var MongoClient = require('mongodb').MongoClient
    , format = require('util').format;    

  MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
    if(err) throw err;

    var collection = db.collection('test_insert');
    collection.insert({a:2}, function(err, docs) {

      collection.count(function(err, count) {
        console.log(format("count = %s", count));
      });

      // Locate all the entries using find
      collection.find().toArray(function(err, results) {
        console.dir(results);
        // Let's close the db
        db.close();
      });      
    });
  })
exebook
  • 32,014
  • 33
  • 141
  • 226