Click here to Skip to main content
15,742,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have for loop in java script function
while calling for loop every success i need to capture the success data.
But while executing For loop after 1st iteration its not waiting for 2nd iteration to execute. Its hitting backend code directly.
Someone kindly suggest me.

What I have tried:

function submit(payload, btn1) {
        for (let x = 0; x <; x++) {
Updated 26-Apr-23 4:20am
Member 15627495 24-Apr-23 1:41am    

why don't you group the first query with all datas 'in' ?
and get back 'one' answer.

use an array to be send, and get back a answer array.

where 'asynchronous' will be hard to manage it's by 'a loop',
you're going hard the way you do.

- prepare the ajax query datas to send ( a big package )
- send your 'all payloads in'
- get the success response
- process the response
Member 15627495 24-Apr-23 1:48am    
// the loop before
for (let x = 0; x <; x++) {

processMultipleTag(sdk[i], payload[x], btn1) {
// do stuff


1 solution

If I've understood your question correctly, you want to wait for each AJAX request to complete before submitting the next one.

That sort of thing is easiest in an async function:
async function - JavaScript | MDN[^]
// Turn the "PT" function into a Promise:
function creditAsync(sdk, tran) {
    return new Promise(function (resolve, reject) {
        sdk.PT('CREDIT', tran).success(resolve).failure(reject);

async function processMultipleTag(sdk, payload, btn1) {
    for (let x = 0; x <; x++) {
        var tran = sdk.tran({
        try {
            const data = await creditAsync(sdk, tran);
        } catch (e) {
Share this answer
Richard Deeming 24-Apr-23 4:28am    
Yeah, that's still not clearing it up.

The code in my solution will issue the first PT request, and wait for it to complete; then issue the second PT request, and wait for it to complete; ... Once all requests are complete, it will call the submit_postback function.

So if that's not what you want, you need to explain clearly and precisely what you do want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900