Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run database insert query in for loop

I am going to save data using for loops after each iteration, I have some task to do ex doSomeWork() but here this function iterates before query return any response,

I want to make this for loop as it should run doSomeTask function and next lop must iterate after query completion as my function depends on some unique value.

for (let i of data.rows) {
    if(i.doc.sync === false || i.doc.syncFail === true) 
    {
        PouchDb.post(i.doc).then((response) => {
            console.log(response);
            doSomeWork(response);
        })
    }
}
like image 968
akshay bagade Avatar asked Jun 15 '26 09:06

akshay bagade


1 Answers

I think you can use async/await for what you want to achieve.

Try this:

for (let i of data.rows) {
    if(i.doc.sync === false || i.doc.syncFail === true) 
    {
        var response = await PouchDb.post(i.doc);
        console.log(response);
        doSomeWork(response);
    }
}
like image 138
Ravi Shankar Bharti Avatar answered Jun 17 '26 22:06

Ravi Shankar Bharti