Questions about export / import and waiting for a function to finish before starting another one

So I want to chain operations in a group of 3 files… and then another group of 5 files…

I suppose I can use import exports to run functions from one file to another… but!!!

If I export a function from one file… can I still run the file as stand alone??

I am asking because I have been getting some errors when I try to run a file while I am also exporting a function from the same file…

Also… what would it be a good way time the functions so they don’t overlap?.. for example… one function needs to create a file… then I might need to do the import of that file… and then another function needs do something with that same file… so… how do I make sure the reading function isn’t starting before the file creating function is finish?

  1. You got any code/examples?
  2. What are the errors your seeing? When asking for help in all situations, do not talk about errors without giving them.
  3. Generally having stuff in different files doesn’t matter regardless of other contexts, but how you import and use stuff does. JavaScript has multiple ways to import/export stuff between files, and multiple ways to run all with their own different rules.
  4. Also… what would it be a good way time the functions so they don’t overlap?.. for example… one function needs to create a file… then I might need to do the import of that file… and then another function needs do something with that same file… so… how do I make sure the reading function isn’t starting before the file creating function is finish?

Generally doing stuff is either synchronous or asynchronous. If your doing just 1 thing (say creating a file, then doing something else) you could use either approach. However if your using the “async” approach you will need your code to “wait” for the process to be complete before continuing.

What your describing is a “race condition”, where multiple async tasks could “race” and create issues since timings are all of.

1 Like

Hmmm… Ok… I think its a problem with the importing address… I think?

Here is sample code:
from test.js

import express from "express";

import dotenv from "dotenv";

import fs from "fs";

dotenv.config();

const app = express();

app.use(express.json());

app.get("/", function (req, res) {

res.render("home");

});

function sayHello() {

console.log("Hello! ");

}

sayHello();

app.listen(3030, () => {

console.log(`all good`);

});

export { sayHello };

Then from test2.js

import { sayHello } from "./test";

import express from "express";

import dotenv from "dotenv";

import fs from "fs";

dotenv.config();

const app = express();

app.use(express.json());

app.get("/", function (req, res) {

res.render("home");

});

function sayHelloAgain() {

console.log("Hello Again! ");

}

sayHelloAgain();

sayHello();

app.listen(3030, () => {

console.log(`searching..`);

});

Error [ERR_MODULE_NOT_FOUND]:
Did you mean to import …/test.js?

node:internal/process/esm_loader:97
internalBinding(‘errors’).triggerUncaughtException(

Ok…

I think I need to add the “.js” at the end of the import…

It also just doesn’t export just the function, but all the settings from the file, such as listen in port 3030… AND it fires right away, which is something I don’t want…

If I am gonna chain this together I need to comment and uncomment the ports if I want to run it as a single file or chain… also need to put the import and control it until another function is done…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.