As far as I understand , one have to use async callbacks for all IO in NodeJS (?)
I'm trying to wrap my head around this example code provied by Microsoft.
They start with a db.open(function() {}); That is, no code is provided in the callback of "open"..
So all code that interacts with the db object will execute before open has completed. How can their sample work?
For me in my own code to be able to write any data to the DB I have to provide the writing code as a callback to the db.open function.
another weird problem when running my code, after the data is written, the nodejs server doesnt exit even though all user code have completed. Why is that?
I don't know Typescript at all, but in general node.js terms:
> How can their sample work?
In app.ts
import db = module("./db")
...
var app = express.createServer();
presumably, the import command triggers db.open(), and the createServer() command takes long enough and calls an async call at least once, so that the db.open() always completes.
> the nodejs server doesnt exit even though all user code have completed. Why is that?
From what you have said:
You are using the callback in db.open
Your app is running to "completion", then only does the callback get executed
Two possibilities (can't comment more because don't know typescript): 1. by using the callback you are executing code in a context that TypeScript cannot handle 2. you are not calling some final function like res.end() or res.send()
If 1. is the case, then you have to either get rid of the callback in db.open, and instead use setTimeout( myFunction, waitTime);
in your app, or find out how TypeScript wraps itself up cleanly and do the following:
var self = this;
db.open( function() {
... // do all the computations you want
self.finalCleanup(); // tell typescript you're done
}