How does node.js execute methods sequentially?

RT. I wrote three ways to read and write files, as follows:

 fs.writeFile(filedir, field, function (err) {
                if (err) {
                  console.log(err);
                } else {
                  console.log("file done!");
                }
              });

fs.appendFile(filedir, , function (err) {
                if (err) {
                  console.log(err);
                } else {
                  console.log("catalog done!");
                }
              });

              //
fs.readFile(fieldir, "utf8", (err, data) => {
                ....

now it"s like this. I save a piece of data in a new file at a time, then maintain a directory file, and then extract the file name of that corresponding file from the directory file and read the saved file. The problem is that every time the file is read before it is saved, the reading is completed, resulting in always reading the wrong file. Is there a way to make the read execute late, or is there any other way to make the three methods execute sequentially? Make sure you save it before you read it.

Mar.13,2021

learn the concept of callback function .


Async waterfall


Asynchronous async, promise chain call


you can use Promise chain call to encapsulate the stored function into a Promise function, and then execute the read content function in its successful resolve. You can refer to the code in my project to encapsulate the upload upload function in https://github.com/guangying1., that is, after the contents of the file must be read, and then execute the send function.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b374c8-2b8d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b374c8-2b8d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?