I have read a lot about passing an array as an function parameter for different return values. But it doesn't work if I have three js file.
main.js
module.exports = function(){
this.deliver = require('./file1.js');
this.start = require('./file2.js');
}
file1.js
module.exports = function(id){
require('./main.js')();
function deliver(id){
console.log('Does deliver() work?:'+id);
var arr = new Array(2);
arr[0] = 'content 1';
arr[1] = 'content 2';
return arr;
}
}
file2.js
require('./main.js')();
start.apply(null, deliver('yes'));
function start(x, y){
console.log('x: '+x);
console.log('y: '+y);
}
Do you have an idea, why deliver is not defined?
@Jose, thanks. Yes, that's the whole code. If everything is in one file does it work. But not in three files...
@Quentin, thanks. Do you have an idea how I could make it work with three file? --> I try to outsource a task from file2 to file1 because I will need it for other tasks too. At the end I need the result of file1 back in file2 to work with it. In my example I have omitted the outsourcing.
Thanks for your hint, Qentin. I have updated the code above. deliver(x) contains now a parameter, that I would like to get back into file2.js. It already arrives file1 but doesn't come back.
I'm still trying to find a solution. I have understood how to pass data from file2 to file1 (or vice versa).
main.js
module.exports = function(){
this.file1 = require('./file1.js');
this.file2 = require('./file2.js');
}
file1.js
require('./main.js')();
//file2.receive('yes', 'content 1');
module.exports = {
deliver : function(id){
console.log('Does deliver() work?:'+id);
return file2.receive(id, 'content 1');
}
}
file2.js
require('./main.js')();
file1.deliver('yes');
module.exports = {
receive : function(x, y){
console.log('x: '+x);
console.log('y: '+y);
}
}
I can start file2 and pass data to file1 if "return file2.receive(id, 'content 1');" is faded out. I also can start with file1 and pass data to file2 as long as "file1.deliver('yes');" is faded out. Why don't work both at the same time?
deliveris still out of scope.