callback - Executing imported Javascript functions in order -
i want execute 2 functions in specific have imported 2 other .js files have made. function needs complete first takes bit of time , 2nd 1 starts before first ended , need files first 1 created work. here's .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 path //and makes png , returns path //to png dostufftopng.dostuff(pngfilepath); //i want "dostuff()" start after "convert()" done.
im pretty sure has callbacks, i'm javascript noob , need help. can work settimeout(), seems "duct tape fix" me. there way more elegant?
edit: wonderful people wanted , asked post this, pdftopng.js:
var spindrift= require('spindrift');//this node module var fs = require('fs'); //makes png pdf in pngfolder , returns path png exports.convert = function(path) { var pdf = spindrift(path); var pathtopng = path.substring(0, path.length-4); //takes off .pdf pathtopng += "_out.png"; //this spindrift's stuff, makes png in dir pngfolder/pathtopng pdf.pngstream(500).pipe(fs.createwritestream("pngfolder/" + pathtopng)); return "pngfolder/" + pathtopng; }
welcome async world of javascript. function callback though created synchronously executed asynchronously. have modify code dostuff executed after know sure convert function has executed. can find how can done @ why variable unaltered after modify inside of function? - asynchronous code reference
Comments
Post a Comment