2

I try to Insert query into sqlite database. But the function Does not work correctly.


const sqlite3 = require('sqlite3');
const { open } = require('sqlite');

async function main() {
    try {
        sqlite3.verbose();
        const ordersDb = await createDbConnection('./test.db');
        const orderProcessed = await orderAlreadyProcessed(ordersDb, "users");
        console.log('four ');
    } catch (error) {
        console.error(error);
    }
}


async function orderAlreadyProcessed(ordersDb, orderNumberStr) {
    try {
        console.log('two ');
        const query = `INSERT INTO users (first_name , last_name , username , password ) VALUES ('fdlfkld' , 'sfhdfjdk' , 'sdfhsdfhsd' , 'sdffs' )`
        const row = await ordersDb.run(query);
        console.log('three ');
    } catch (error) {
        console.error(error);
        throw error;
    }
}

function createDbConnection(filename) {
    return open({
        filename,
        driver: sqlite3.Database
    });
}

let one = [ 1 , 2, 3 ]

Promise.all(one.map(async()=>{
    await main();
}))

enter image description here

i need output like this

two
three
four
two
three
four
two
three
four

Sqlite does not work correctly , i need some help to rid the problem

  • Welcome to Stackoverflow: please remember to read the [posting guidelines](/help/how-to-ask), and then [edit] your post to not use pictures of text. Just put the text in your post, with appropriate formatting (e.g. code fences). As for the question: Promise.all does not run "one by one". Any time your code has an `await` is a point where you've told JS that it can skip to whatever else it still needs to do. – Mike 'Pomax' Kamermans Dec 17 '22 at 06:11

0 Answers0