0

I want to execute 2 functions in a specific that I have imported from 2 other .js files that I have made. The function that needs to complete first takes a bit of time and the 2nd one starts before the first is ended and I need files the first one created for it to work. Here's basically what my .js looks like:

var pdfToPng = require("./pdfToPng.js");
var doStuffToPng = require("./doStufftoPng.js");

var pdfFilePath = process.argv[2];


var pngFilePath = pdftoPng.convert(PdfFilePath);//convert takes a path 
                                        //and makes a png and returns path
                                        //to the png
doStuffToPng.doStuff(pngFilePath);
//I want "doStuff()" to start AFTER "convert()" is done.

Im pretty sure it has something to do with callbacks, but I'm a javascript noob and need help. I can get it to work with setTimeout(), but that seems like a "duct tape fix" to me. Is there some way more elegant?

Edit: some wonderful people wanted to help and asked to post this, the pdfToPng.js:

var spindrift= require('spindrift');//this is a node module
var fs = require('fs');

//Makes a png from pdf in pngFolder and returns the path to that png
exports.convert = function(path)
{
   var pdf = spindrift(path);
   var pathToPng = path.substring(0, path.length-4); //takes off the .pdf
   pathToPng += "_out.png";

   //this is spindrift's stuff, makes a png in dir pngFolder/pathToPng
   pdf.pngStream(500).pipe(fs.createWriteStream("pngFolder/" + pathToPng));
   return "pngFolder/" + pathToPng;
}

3 Answers3

1

Welcome to the async world of javascript. The function callback though created synchronously is executed asynchronously. So you have to modify the code to get doStuff executed only after you know for sure that convert function has executed. You can find how this can be done @ Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

Community
  • 1
  • 1
meteor
  • 2,518
  • 4
  • 38
  • 52
0

if so, you need to implement your own callback, - Open pdftoPNG.js - modify convert function with one more parameter

function convert(PdfFilePath, finishConvert) {
    //now just insert  this line where you finally instead of return
    //remove return yourpngpath; //or something, i assume 
    //add following in the place of return
    finishConvert(yourpngpath);
}

Then Please call like this

var pdfToPng = require("./pdfToPng.js");
var doStuffToPng = require("./doStufftoPng.js");

var pdfFilePath = process.argv[2];


pdftoPng.convert(PdfFilePath,function(path){

   if(path!="") {
       doStuffToPng.doStuff(path);
   }
});
Sagar Devkota
  • 1,192
  • 8
  • 13
  • 1
    I'm not using github.com/thnew/Pdf2Png. I am using my own files. Inside the pdfToPng.js i do use the node module "spindrift" but im also doing other things. And I'd like to keep those 2 imported files independent. If i use a callback wouldn't that make one of them dependent on the other? – Colter Anderson Mar 17 '15 at 03:33
  • I think solution is callback, can you check the latest modification and implement to your code? – Sagar Devkota Mar 17 '15 at 04:20
  • @ColterAnderson. It doesn't make them dependent. By passing in a callback function to the `pdfToPng.js` file, you're allowing the `convert` method to execute a new function (the callback). The `convert` method doesn't need to know what the function does, so there is no dependency – jasonscript Mar 17 '15 at 04:39
0

You have to update your convert method to support callbacks/ promises.

Here is an example using Callbacks

exports.convert = function(path, fnCallback)
{
   var pdf = spindrift(path);
   var pathToPng = path.substring(0, path.length-4); //takes off the .pdf
   pathToPng += "_out.png";

   //this is spindrift's stuff, makes a png in dir pngFolder/pathToPng
   pdf.pngStream(500).pipe(fs.createWriteStream("pngFolder/" + pathToPng));

   if (fnCallback && typeof(fnCallback) === "function") {
      fnCallback("pngFolder/" + pathToPng); 
   }
}

You'll see the following

  • A new parameter being passed in fnCallback
  • A check to make sure that the parameter is passed in and that it is a function
  • The fnCallback function gets called with the results passed in as a parameter
  • Removing of the return statement

Now when the convert method is called, after the long running process completes, the Callback method will get executed.

To call the modified convert method you now have to pass in a callback function

function myCallback(path){
    // do something with the path
}

pdftoPng.convert(PdfFilePath,myCallback);
jasonscript
  • 6,039
  • 3
  • 28
  • 43