Nodejs PDF KIT, pdf file is in use by application

Sample code I have tried:

var PDFDocument = require('pdfkit');
var fs = require('fs');
var doc = new PDFDocument;

doc.pipe(fs.createWriteStream('test.pdf'));
doc.text('hello how r u');

var x= true;
doc.end();

while(x){
    console.log("***");
}

This code is not releasing the test.pdf and thereby I am not able to open the file. How do I get the file released so that it can be used by rest of the application?

This issue has been discussed on github. The recommended way of doing something when writing to the stream is finished is to watch the finish event on the stream:

stream.on( 'finish', function() { ..... } );