Domanda

im having problems understanding how to talk 'up and down' between app.js and modules...

I think its with a callback but i've also seen things like self._send(), this.send() and module.exports.emit

I'm quite confused.

I recently installed pdfkit from npm (quite good 6/10 :p) I want to learn by improving it slightly though by adding a done event/callback for doc.write().

I know its not that important but i've been looking through my installed modules and that is probably the easiest example of code that wouldn't hurt to have a 'DONE' I also figured this function would be good to learn from as it uses fs.writeFile which has a function(){} that fires when its finished writing so the fact that i can see where in the code it ends makes it an easy learning tool.

I've modified the code a few times tried to compare modules to see where similar things have been done but i just keep breaking it with errors, i don't feel like i'm getting anywhere:

inside the pdfkit module document.js i've made changes:

var EventEmitter = require('events').EventEmitter;//ben
module.exports = new EventEmitter();//ben


PDFDocument.prototype.write = function(filename, fn, callback) {//ben added callback
  return this.output(function(out) {
    return fs.writeFile(filename, out, 'binary', fn, function(){//ben added finished function
      //module.exports.emit('pdf:saved');//ben
      callback();//ben
      });
  });
};

in my app.js:

doc.write('public_html/img/'+_.c+'_'+_.propertyid+'.pdf',function(){console.log('pdf:saved');});

//doc.on('pdf:saved',function(){console.log('pdf:saved');});

I'm also not really sure what i'm querying on google, please can someone help me?

È stato utile?

Soluzione 2

FIXED

fn is the callback function!

PDFDocument.prototype.write = function(filename, fn) {
  return this.output(function(out) {
    return fs.writeFile(filename, out, 'binary', fn);
  });
};

and by naming my callback function to fn it works!

doc.write('public_html/img/'+_.c+'_'+_.propertyid+'.pdf',function fn(){console.log('pdf:saved');});

for me that is a massive learning mountain climbed!!

Altri suggerimenti

EventEmitter is required to create objects with event storage, and emit/catch events.
While what you are using in your example is called 'callback'.

It is two different ways, and can be sort of cross used. Sometimes it is good to use events, but sometimes just callback is enough.

The best way to have head around it: play with callbacks (forget about events for now). Try to think of different uses of callbacks and maybe even have callback function and pass it around. Then come to callback chains. And only after start playing with EventEmitter. Remember that EventEmitter is different thing from callbacks, with sometimes compatible use cases, but generally is used in different cases.

Here is your code, simplified with same functionality as you have/need atm:

PDFDocument.prototype.write = function(filename, callback) {
  this.output(function(out) {
    fs.writeFile(filename, out, 'binary', callback);
  });
};

And use it the way you already do.
Do not try to generate garbage of code that will only complicate everything - it is better to study speficic areas seperatelly, and then switch to next one. Otherwise will mess up in mind.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top