0

my code is:

 var json = [1,2,3,4,5,6,7,8,9];
 async.forEach(json, function (that, callback) {
      sqlitedb.run("INSERT INTO mytable (name) VALUES ("+that+")" , function () {
           console.log(that); callback();
      });
 });

The callback() placed on after insert item. but when i execute, my json items inserted on no squence and result is like:

7
8
9
4
5
3
6
1
2

Can anyone help me. Thanks.

1
  • Note that queries without an ORDER BY clause aren't guaranteed to have any particular ordering anyway. Commented Jun 9, 2015 at 19:58

1 Answer 1

1

You have to use async.eachSeries:

forEach will execute in parallel.

async.eachSeries(json, function (that, callback) {
      sqlitedb.run("INSERT INTO mytable (name) VALUES ("+that+")" , function () {
           console.log(that); callback();
      });
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.