-1

What is the best way to make javascript wait for the current iteration of the for loop to finish before the next one?

Heres my problem.

At the moment, I am running an SQL select statement from a SQLite database for each item in a for loop, like this.

for(let i = 0; i < myresult.length; i++){
    var query = "SELECT * FROM table WHERE id = " + myresult[i].id;
    var result = //Run sql here    
    if(result.length == 0){ // if result doesn't exist, make it.
       var insertQuery = "INSERT INTO table";
    }
}

My issue is that as it runs through the loop, it starts to run the insert statement before the select is complete.

For example

//First iteration of loop

"SELECT * FROM table WHERE id = 1";

"INSERT INTO table (id) VALUES (1)";

//Second iteration of loop

"SELECT * FROM table WHERE id = 2";

"INSERT INTO table (id) VALUES (1)";

It is doing this because its not waiting for the current iteration of the for loop to finish, before starting the next.

How do I make it so that the for loop waits for the current loop to finish, before starting a new one?

S_R
  • 1,818
  • 4
  • 27
  • 63
  • The SQL queries are asynchronous. They probably have some promise or callback mechanism. – Ori Drori Apr 05 '18 at 12:01
  • Sync would have blocked the thread, and you wouldn't have the problem. What are you using to connect to SQL? Browser or Node? – Ori Drori Apr 05 '18 at 12:06
  • How is this line really executed? `var result = //Run sql here` ... if that one doesn't block until done, e.g. fire up an AJAX call, you'll end up with such issue. – Asons Apr 05 '18 at 12:07
  • Maybe use a [generator](https://www.promisejs.org/generators/)? – zer00ne Apr 05 '18 at 12:07
  • 1
    You omitted the most important line: var result = //Run sql here Please show your code. – Rob Apr 05 '18 at 12:19
  • If I only had a dime every time this type of async questions were asked... – nicholaswmin Apr 05 '18 at 12:35
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – nicholaswmin Apr 05 '18 at 12:36

1 Answers1

-1

It will be Better if you Use Store Procedure in ms-sql and bind your data in JSON on Javascript using ajax/angular.

DECLARE @TEMPTABLE TABLE (

                                              field1 BIGINT, -- ID
                                              field2 NVARCHAR(100), 
                                              field3 NVARCHAR(150), 
                                              field4 NVARCHAR(150) 
                     );

INSERT @tempTable (

          field1, 
          field2, 
          field3, 
          field4 
   ) 

SELECT t.field1,

   t.field2,

   t.field3, 

   t.field4 

FROM table1 t

DECLARE @CountTable INT = (

   SELECT Count(*) 

   FROM   @tempTable t 

);

IF (@CountTable <= 0)

BEGIN

SELECT 'NO RECORD FOUND' AS [STATUS];

RETURN;

END;

DECLARE @count INT;

SET @count = 1;

WHILE (@count <= @CountTable)

BEGIN

DECLARE @Field1 NVARCHAR(100) = '',

@Field2       NVARCHAR(150) = '', 
@Field3       NVARCHAR(150) = '', 
@Field4       NVARCHAR(150) = '', 

select top 1

     @Field1 = t.field1, 
     @Field2 = t.field2, 
     @Field3 = t.field3, 
     @Field4 = t.field4 

FROM @tempTable t;

DELETE @tempTable

WHERE field1 = @Field1; -- ID

SET @count = @count + 1; END;

Jay Raj Mishra
  • 110
  • 2
  • 9