0

I have a function that looks like this:

const XML = '.xml code';

var array = [];
var i = 0; //counter

XMLExtract(XML, 'loc', false, (error, element) => {
    if (error) {
        throw new Error(error);
    }

    array[i] = element; 

    console.log(array[i]);

    function_name(array[i]); //imported function from external .js

    i++;

});

Basically I want to run function() to return the response that it gives and then run function() again with the new parameter. However, the above code doesn't work, it just overlaps.

I've also checked previous solutions: https://stackoverflow.com/a/5010339 but I think I don't really know how to implement it. Any suggestion?

UPDATE: external.js

module.exports = function() {

this.function_name = function() {

(async function() {

 ...

await instance.exit();

}());
};
};
Kevin
  • 203
  • 1
  • 8
  • The link you included has the answer right there. Edit the function in `external.js` and include a callback parameter, then run that callback at the end of the function. – victor Jun 29 '17 at 22:10
  • 1
    1) I wouldn't name a function `function`. 2) You're only calling the function once. If I understand correctly, you're looking to do `someFunction(element, someOtherFunction);`? 3) You can get rid of `i` and just do `array.push`. – Brett Beatty Jun 29 '17 at 22:12
  • @BrettBeatty 1) It was just for reference purpose. 2) `XMLExtract(XML, 'loc', false, (error, element)` is actually a loop and I'm looking to first run `function()`, wait until it finishes, then increase the counter by 1 and then run the same function with the new parameter. 3) I will do it, thanks :) – Kevin Jun 29 '17 at 22:16
  • @VictorC. I mean I don't really understand it as I'm a newbie in Javascript/Node.js. If you could help me adjusting it using the code above, it would help a lot. – Kevin Jun 29 '17 at 22:19
  • "*`// This is a loop`*" - no, it's a comment. Please post your actual code. If you meant recursion (which is the correct approach indeed), where do you define `function_name`? – Bergi Jun 29 '17 at 22:26
  • Sorry for misunderstanding, I know it sounds weird, but forgive me as I'm a newbie in Javascript/Node.js. I was referring to `XMLExtract()` because basically when you pass in the parameter XML (which is in this case is a .xml code), it outputs the urls. I've updated the code. – Kevin Jun 29 '17 at 22:33
  • I'm having a hard time reading all of your functions, but if you return the result of the call to the async function in `external.js` from `this.function_name`, you could then in your main code do `function_name(element).then(function_name)` and have it called again with its result. – Brett Beatty Jun 29 '17 at 22:34
  • Take a look into promises. It may help you. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise – MNF Jun 29 '17 at 22:36
  • 1
    Callbacks & promises are the core of asynchronous development. You really need to understand those. – Seblor Jun 29 '17 at 22:50
  • I'm actually stuck trying to recreate what I think is your issue in this simple example it looks like you are getting the behavior you are looking for https://repl.it/JIGV/5 am I missing something? – Jason Sperske Jun 29 '17 at 22:53

0 Answers0