0
function Getir() {
    var options =
    {
        host: 'example',
        port: 443,
        path: '/myUrl'
    };

    get(options, function (http_res) {
        var data = "";


        http_res.on("data", function (chunk) {

            data += chunk;
        });

        
        http_res.on("end", function () {
            
            writeFile('NewHtml.txt', `${data}`, 'utf8', (err) => {
                if (err) console.log(err);
            });
        });
    });
}


function DegistirDuzenle() {
    if (existsSync("./DatabaseHtml.txt")) {

        var DataBaseHtml = readFileSync("./DatabaseHtml.txt", 'utf-8', (err) => { if (err) console.log(err) });
        var MyHtml = readFileSync("./NewHtml.txt", 'utf-8', (err) => {if (err) console.log(err) });

        if (MyHtml == DataBaseHtml) {
            unlink("./NewHtml.txt", (err)=>{ if(err) console.log(err)});
            console.log("değişiklik yapılmadı");
        } else {
            //notification

            console.log("değişiklik yapıldı");

            //Change
            unlink('./DatabaseHtml.txt', (err) => { if(err) console.log(err); });
            writeFile('./DatabaseHtml.txt', `${MyHtml}`, 'utf-8', (err) => { if(err) console.log(err); });
            unlink('./NewHtml.txt', (err) => { if(err) console.log(err); });
        }

    }
    else {
        writeFile('DatabaseHtml.txt', `NewDataBaseHtml`, 'utf8', (err) => {
            if (err) console.log(err);
        });
    }
}

async function Mysystem() {
    let mypromis = new Promise((resolve, reject)=>{
        resolve(Getir());
    });
    await mypromis.then(DegistirDuzenle());
}
Mysystem();

I want to create a txt file, read it and delete it later. I have 2 function 1.(Getir()) Create txt, 2.(DegistirDuzenle()) read txt and delete but 2. function starts working first and I getting error. "Error: ENOENT: no such file or directory, open './NewHtml.txt'"

  • Is your Getir function async ? – Amirhossein Nov 28 '22 at 15:51
  • 2
    any chance of more context, what the errors actually are, or maybe a sandbox link? Check out the [How do I ask a good question? post](https://stackoverflow.com/help/how-to-ask) to learn how to better structure a question. It's beneficial for everyone. – damonholden Nov 28 '22 at 15:53
  • You are using both ways to handle promises. Can you give us tell us if Getir and DegistirDuzenle are also async functions ? – Hedi Zitouni Nov 28 '22 at 15:58
  • Please also post the code of `Getir` and `DegistirDuzenle`. You can [edit] your question to include them. – Bergi Nov 28 '22 at 16:03

2 Answers2

0
async function Mysystem() {
    let mypromis = new Promise((resolve, reject)=>{
        resolve(Getir());
    });
    await mypromis()
    await DegistirDuzenle()
}

Mysystem()
İsmail
  • 45
  • 3
0

You should use

async function Mysystem() {
    await Getir();
    await DegistirDuzenle();
}

or

function Mysystem() {
    return Getir().then(DegistirDuzenle);
}

but not a mix of them. Also notice that when passing the DegistirDuzenle function to .then() as a callback, it shouldn't be invoked (passing the result of a call, not passing the function). Alternatively, you could write .then((getirResult) => DegistirDuzenle()).


Also, for this to work, you'll need to properly promisify the code in Getir and DegistirDuzenle.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375