0

I have a series of if statements in a loop like this:

for( var i = 0; i < results_list.length; i++){
find = await results_list[i];    
    //result 1
    if (find.Process == "one") {
        await stored_proc(38, find.Num, find.Status)
    }
    //result 2
    if(find.Process == "two") {
        await stored_proc(37, find.Num, find.Status)
    }
    //result 3
    if(find.Process == "three") {
        await stored_proc(39, find.Num, find.Status)
    }
}

My issue is that it runs all of these synchronously causing my stored procedure to trip over itself. How can I ensure each if statement waits for the previous one to complete before running?

It is also important to know that each if statement is not always run, for instance on one run of my code //result 2 may run and //result 1 and //result 3 may not run.

Sometimes they might all run and sometimes none will run at all.

Thanks for any help!

EDIT: Here is my stored procedure function

async function stored_proc(opID, num, stat){
         sql.executeTransaction( connection, {
            procedure: "<stored procedure>",
            params: { 
                OpID: {
                    val: opID,
                    type: sql.INT
                },
                num: {
                    val: num,
                    type: sql.STRING
                },
                Pass: {
                    val: stat,
                    type: sql.INT
                },
                ExtraData: {
                    val: "upload",
                    type: sql.STRING
                }
            }
        } ).then( async function( data ) {
            return data.transaction
                .commit()
                .then( async function() {
                    console.log("Updated database...." );
                } );
        }, function( err ) {
            console.log( err );
        } );
    }

SECOND EDIT: I have looked into this some more and found that the if there is more than one result to upload it will NEVER upload the first sets of results. I have ran some console.log()s through and found it will always get find.Num and find.Status. It will only log Updated database for every result after the first one. I hope this makes sense

12
  • 4
    Put that code within an async function. Commented Jul 1, 2019 at 13:35
  • @Ele ah sorry, I skipped that part when writing the question. In my code it is in an async function Commented Jul 1, 2019 at 13:35
  • 1
    WRT the title - don't you mean make them synchronous? They seem to already by asynchronous, so making them "more asynchronous" seems a bit strange. Commented Jul 1, 2019 at 13:37
  • 1
    First of all, you are trying to make it wait for asynchronous operations, you will never make them really synchronous. The await keyword will only make the code look synchronous. Second of all, your calling function from your linked question is not marked as async, that's likely your problem Commented Jul 1, 2019 at 13:40
  • 3
    The code you posted works. If it doesn't wait for the stored procedure results, it's the fault of the stored_proc function not returning a promise that fulfills at the right time. Commented Jul 1, 2019 at 13:43

1 Answer 1

2

In your stored_proc you are not returning the Promise.
Also, promise inside promise is considered anti-pattern you can chain it easily.

async function stored_proc(opID, num, stat) {
    return sql.executeTransaction(connection, {
        procedure: "<stored procedure>",
        params: {
            OpID: {
                val: opID,
                type: sql.INT
            },
            num: {
                val: num,
                type: sql.STRING
            },
            Pass: {
                val: stat,
                type: sql.INT
            },
            ExtraData: {
                val: "upload",
                type: sql.STRING
            }
        }
    })
    .then(function (data) {
        return data.transaction
            .commit()
    })
    .then(function () {
        console.log("Updated database....");
    })
    .catch((err) => {
        console.log(err);
    })
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'd go further and convert the inner function to awaits over .then()s too, but your answer is correct :)
Thanks for the response however it still doesn't work, it still doesn't upload the first result it finds to the database.

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.