I have created in my project several separate functions in separate files that I import with module.exports.$functionName
I then compile everything in an index.js file. This is the problem. The first function allows to download a file, without which the two other functions can't work. I tried to use the promises to wait for the download, but the file does not download and this error appears:
Error: ENOENT: no such file or directory
Here is the code, I specify that each function works when you run them independently:
const mt = require("./mainTweet"); // from mainTweet.js
const rp = require("./Reply"); // from Reply.js
const od = require("./getcsv") // from getcsv.js
function Download() {
return new Promise((resolve) => {
od.onDownload();
resolve();
});
}
function firstTweet() {
return new Promise((resolve) => {
mt.getData()
resolve();
});
}
function secondTweet() {
rp.Reply();
}
async function Launch() {
await Download();
await FirstTweet();
Reply();
}
Launch();
Since you answered in a comment that od.onDownload, mt.getData and rp.Reply are async, the problem is that you do not await them.
When you do this:
od.onDownload()
resolve()
onDownload() will run and return a promise immediately and then you resolve your own promise. This means that onDownload has not yet resolved (or thrown). You should instead await each call. You could simplify your whole code to this:
const mt = require("./mainTweet"); // from mainTweet.js
const rp = require("./Reply"); // from Reply.js
const od = require("./getcsv") // from getcsv.js
async function Launch() {
await od.Download();
await mt.getData();
rp.Reply()
}
Launch();