Correct way of using Async/await in node.js











up vote
0
down vote

favorite












I have written some code to call a procedure,it's OUT parameter type is cursor(ResultSet),so i have to fetch data from ResultSet,for that i've written one function(fetchRowsFromRS()) which extract data from ResultSet.




  1. I've used return statement in fetchRowsFromRS(),but not returning
    anything, getting undefined.

  2. When i called fetchRowsFromRS(),control is not pausing execution
    of next lines of code(i'have used Async/await) this is required because i want use extracted data in next lines.


What is the mistake in my code?



db.js



    connection.execute(plsql,bindvars,options,async function (err, result) {
if (err) {
console.log(err);
console.error(err.message);
doRelease(connection);
return;
}

if(result.outBinds.OUT_STATUS=='SUCCESS'){

if(result.outBinds.OUT_MENU_NAME.metaData.length=0){

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
}else{

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User
logged in successfully');
var numRows=20;
//calling function to fetch data from ResultSet
var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
console.log('----------'+rsData);//giving undefined

//here i want to use ResultSet Data

}
}
})


function to extract data from ResultSet(not returning anything)



function fetchRowsFromRS(connection, resultSet, numRows) {
resultSet.getRows(numRows,function (err, rows) {
if (err) {
console.error(err);
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length > 0) {
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
console.log(rows); //getting data here


if (rows.length === numRows) // might be more rows
fetchRowsFromRS(connection, resultSet, numRows);
else
doClose(connection, resultSet); // always close the ResultSet


} else { // no rows
doClose(connection, resultSet); // always close the ResultSet
}
return rows;
});
}









share|improve this question







New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • "I've used return statement in fetchRowsFromRS": no, you have not. You have a return in a callback function, which is a different function.
    – trincot
    Nov 18 at 14:08










  • What is connection.execute? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an async function as a callback.
    – Bergi
    Nov 18 at 14:30






  • 3




    Possible duplicate of How to use async await function object in Javascript?
    – NAVIN
    Nov 18 at 14:56










  • There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem.
    – NAVIN
    Nov 18 at 14:57










  • @NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js
    – viswanath
    Nov 18 at 17:00















up vote
0
down vote

favorite












I have written some code to call a procedure,it's OUT parameter type is cursor(ResultSet),so i have to fetch data from ResultSet,for that i've written one function(fetchRowsFromRS()) which extract data from ResultSet.




  1. I've used return statement in fetchRowsFromRS(),but not returning
    anything, getting undefined.

  2. When i called fetchRowsFromRS(),control is not pausing execution
    of next lines of code(i'have used Async/await) this is required because i want use extracted data in next lines.


What is the mistake in my code?



db.js



    connection.execute(plsql,bindvars,options,async function (err, result) {
if (err) {
console.log(err);
console.error(err.message);
doRelease(connection);
return;
}

if(result.outBinds.OUT_STATUS=='SUCCESS'){

if(result.outBinds.OUT_MENU_NAME.metaData.length=0){

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
}else{

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User
logged in successfully');
var numRows=20;
//calling function to fetch data from ResultSet
var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
console.log('----------'+rsData);//giving undefined

//here i want to use ResultSet Data

}
}
})


function to extract data from ResultSet(not returning anything)



function fetchRowsFromRS(connection, resultSet, numRows) {
resultSet.getRows(numRows,function (err, rows) {
if (err) {
console.error(err);
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length > 0) {
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
console.log(rows); //getting data here


if (rows.length === numRows) // might be more rows
fetchRowsFromRS(connection, resultSet, numRows);
else
doClose(connection, resultSet); // always close the ResultSet


} else { // no rows
doClose(connection, resultSet); // always close the ResultSet
}
return rows;
});
}









share|improve this question







New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • "I've used return statement in fetchRowsFromRS": no, you have not. You have a return in a callback function, which is a different function.
    – trincot
    Nov 18 at 14:08










  • What is connection.execute? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an async function as a callback.
    – Bergi
    Nov 18 at 14:30






  • 3




    Possible duplicate of How to use async await function object in Javascript?
    – NAVIN
    Nov 18 at 14:56










  • There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem.
    – NAVIN
    Nov 18 at 14:57










  • @NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js
    – viswanath
    Nov 18 at 17:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have written some code to call a procedure,it's OUT parameter type is cursor(ResultSet),so i have to fetch data from ResultSet,for that i've written one function(fetchRowsFromRS()) which extract data from ResultSet.




  1. I've used return statement in fetchRowsFromRS(),but not returning
    anything, getting undefined.

  2. When i called fetchRowsFromRS(),control is not pausing execution
    of next lines of code(i'have used Async/await) this is required because i want use extracted data in next lines.


What is the mistake in my code?



db.js



    connection.execute(plsql,bindvars,options,async function (err, result) {
if (err) {
console.log(err);
console.error(err.message);
doRelease(connection);
return;
}

if(result.outBinds.OUT_STATUS=='SUCCESS'){

if(result.outBinds.OUT_MENU_NAME.metaData.length=0){

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
}else{

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User
logged in successfully');
var numRows=20;
//calling function to fetch data from ResultSet
var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
console.log('----------'+rsData);//giving undefined

//here i want to use ResultSet Data

}
}
})


function to extract data from ResultSet(not returning anything)



function fetchRowsFromRS(connection, resultSet, numRows) {
resultSet.getRows(numRows,function (err, rows) {
if (err) {
console.error(err);
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length > 0) {
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
console.log(rows); //getting data here


if (rows.length === numRows) // might be more rows
fetchRowsFromRS(connection, resultSet, numRows);
else
doClose(connection, resultSet); // always close the ResultSet


} else { // no rows
doClose(connection, resultSet); // always close the ResultSet
}
return rows;
});
}









share|improve this question







New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have written some code to call a procedure,it's OUT parameter type is cursor(ResultSet),so i have to fetch data from ResultSet,for that i've written one function(fetchRowsFromRS()) which extract data from ResultSet.




  1. I've used return statement in fetchRowsFromRS(),but not returning
    anything, getting undefined.

  2. When i called fetchRowsFromRS(),control is not pausing execution
    of next lines of code(i'have used Async/await) this is required because i want use extracted data in next lines.


What is the mistake in my code?



db.js



    connection.execute(plsql,bindvars,options,async function (err, result) {
if (err) {
console.log(err);
console.error(err.message);
doRelease(connection);
return;
}

if(result.outBinds.OUT_STATUS=='SUCCESS'){

if(result.outBinds.OUT_MENU_NAME.metaData.length=0){

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
}else{

loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');

loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User
logged in successfully');
var numRows=20;
//calling function to fetch data from ResultSet
var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
console.log('----------'+rsData);//giving undefined

//here i want to use ResultSet Data

}
}
})


function to extract data from ResultSet(not returning anything)



function fetchRowsFromRS(connection, resultSet, numRows) {
resultSet.getRows(numRows,function (err, rows) {
if (err) {
console.error(err);
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length > 0) {
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
console.log(rows); //getting data here


if (rows.length === numRows) // might be more rows
fetchRowsFromRS(connection, resultSet, numRows);
else
doClose(connection, resultSet); // always close the ResultSet


} else { // no rows
doClose(connection, resultSet); // always close the ResultSet
}
return rows;
});
}






javascript node.js async-await






share|improve this question







New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 18 at 13:59









viswanath

83




83




New contributor




viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






viswanath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • "I've used return statement in fetchRowsFromRS": no, you have not. You have a return in a callback function, which is a different function.
    – trincot
    Nov 18 at 14:08










  • What is connection.execute? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an async function as a callback.
    – Bergi
    Nov 18 at 14:30






  • 3




    Possible duplicate of How to use async await function object in Javascript?
    – NAVIN
    Nov 18 at 14:56










  • There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem.
    – NAVIN
    Nov 18 at 14:57










  • @NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js
    – viswanath
    Nov 18 at 17:00


















  • "I've used return statement in fetchRowsFromRS": no, you have not. You have a return in a callback function, which is a different function.
    – trincot
    Nov 18 at 14:08










  • What is connection.execute? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an async function as a callback.
    – Bergi
    Nov 18 at 14:30






  • 3




    Possible duplicate of How to use async await function object in Javascript?
    – NAVIN
    Nov 18 at 14:56










  • There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem.
    – NAVIN
    Nov 18 at 14:57










  • @NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js
    – viswanath
    Nov 18 at 17:00
















"I've used return statement in fetchRowsFromRS": no, you have not. You have a return in a callback function, which is a different function.
– trincot
Nov 18 at 14:08




"I've used return statement in fetchRowsFromRS": no, you have not. You have a return in a callback function, which is a different function.
– trincot
Nov 18 at 14:08












What is connection.execute? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an async function as a callback.
– Bergi
Nov 18 at 14:30




What is connection.execute? Given that it takes a node-style callback, it doesn't appear to promise-aware. Do not pass an async function as a callback.
– Bergi
Nov 18 at 14:30




3




3




Possible duplicate of How to use async await function object in Javascript?
– NAVIN
Nov 18 at 14:56




Possible duplicate of How to use async await function object in Javascript?
– NAVIN
Nov 18 at 14:56












There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem.
– NAVIN
Nov 18 at 14:57




There are so many examples and tutorials why don't you invest time learning basics of javascript. You didn't used async-await and just asking other to fix your problem.
– NAVIN
Nov 18 at 14:57












@NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js
– viswanath
Nov 18 at 17:00




@NAVIN Sorry about that, I am in the learning process of understanding javascript and node.js
– viswanath
Nov 18 at 17:00












2 Answers
2






active

oldest

votes

















up vote
0
down vote













fetchRowsFromRS is not returning anything. You don't seem to be using async/await correctly. resultSet.getRows uses a callback. Change it to return a Promise or wrap it in a Promise and return that from fetchRowsFromRS. Then you'll be able to await it.






share|improve this answer




























    up vote
    0
    down vote













    function fetchRowsFromRS(connection, resultSet, numRows) {
    return resultSet
    .getRows(numRows)
    .then(function(rows) {
    if (rows.length > 0) {
    console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
    console.log(rows) //getting data here
    if (rows.length === numRows)
    // might be more rows
    fetchRowsFromRS(connection, resultSet, numRows)
    else doClose(connection, resultSet) // always close the ResultSet
    } else {
    // no rows
    doClose(connection, resultSet) // always close the ResultSet
    }
    return rows
    })
    .catch(function(err) {
    if (err) {
    console.error(err)
    doClose(connection, resultSet) // always close the ResultSet
    }
    })
    }


    The above will work if, resultSet.getRows(numRows) returns a promise instead of accepting a callback or there is some alternate for resultSet.getRows(numRows) which return a promise.




    await fetchRowsFromRS(connection, resultSet, numRows)




    will work only if fetchRowsFromRS(connection, resultSet, numRows) returns a promise, which in turn requires everything defines inside the function to return a promise.



    OR



    Use new Promise



    function fetchRowsFromRS(connection, resultSet, numRows) {
    return new Promise(function(resolve, reject) {
    resultSet.getRows(numRows, function(err, rows) {
    if (err) {
    console.error(err)
    doClose(connection, resultSet) // always close the ResultSet
    reject(err)
    } else if (rows.length > 0) {
    console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
    console.log(rows) //getting data here

    if (rows.length === numRows)
    // might be more rows
    fetchRowsFromRS(connection, resultSet, numRows)
    else doClose(connection, resultSet) // always close the ResultSet
    } else {
    // no rows
    doClose(connection, resultSet) // always close the ResultSet
    }
    resolve(rows)
    })
    })
    }





    share|improve this answer





















    • Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
      – Bergi
      Nov 18 at 15:23










    • @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
      – viswanath
      Nov 19 at 10:24











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    viswanath is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361690%2fcorrect-way-of-using-async-await-in-node-js%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    fetchRowsFromRS is not returning anything. You don't seem to be using async/await correctly. resultSet.getRows uses a callback. Change it to return a Promise or wrap it in a Promise and return that from fetchRowsFromRS. Then you'll be able to await it.






    share|improve this answer

























      up vote
      0
      down vote













      fetchRowsFromRS is not returning anything. You don't seem to be using async/await correctly. resultSet.getRows uses a callback. Change it to return a Promise or wrap it in a Promise and return that from fetchRowsFromRS. Then you'll be able to await it.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        fetchRowsFromRS is not returning anything. You don't seem to be using async/await correctly. resultSet.getRows uses a callback. Change it to return a Promise or wrap it in a Promise and return that from fetchRowsFromRS. Then you'll be able to await it.






        share|improve this answer












        fetchRowsFromRS is not returning anything. You don't seem to be using async/await correctly. resultSet.getRows uses a callback. Change it to return a Promise or wrap it in a Promise and return that from fetchRowsFromRS. Then you'll be able to await it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 14:07









        Sergiu Paraschiv

        8,47432741




        8,47432741
























            up vote
            0
            down vote













            function fetchRowsFromRS(connection, resultSet, numRows) {
            return resultSet
            .getRows(numRows)
            .then(function(rows) {
            if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here
            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            return rows
            })
            .catch(function(err) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            }
            })
            }


            The above will work if, resultSet.getRows(numRows) returns a promise instead of accepting a callback or there is some alternate for resultSet.getRows(numRows) which return a promise.




            await fetchRowsFromRS(connection, resultSet, numRows)




            will work only if fetchRowsFromRS(connection, resultSet, numRows) returns a promise, which in turn requires everything defines inside the function to return a promise.



            OR



            Use new Promise



            function fetchRowsFromRS(connection, resultSet, numRows) {
            return new Promise(function(resolve, reject) {
            resultSet.getRows(numRows, function(err, rows) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            reject(err)
            } else if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here

            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            resolve(rows)
            })
            })
            }





            share|improve this answer





















            • Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
              – Bergi
              Nov 18 at 15:23










            • @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
              – viswanath
              Nov 19 at 10:24















            up vote
            0
            down vote













            function fetchRowsFromRS(connection, resultSet, numRows) {
            return resultSet
            .getRows(numRows)
            .then(function(rows) {
            if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here
            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            return rows
            })
            .catch(function(err) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            }
            })
            }


            The above will work if, resultSet.getRows(numRows) returns a promise instead of accepting a callback or there is some alternate for resultSet.getRows(numRows) which return a promise.




            await fetchRowsFromRS(connection, resultSet, numRows)




            will work only if fetchRowsFromRS(connection, resultSet, numRows) returns a promise, which in turn requires everything defines inside the function to return a promise.



            OR



            Use new Promise



            function fetchRowsFromRS(connection, resultSet, numRows) {
            return new Promise(function(resolve, reject) {
            resultSet.getRows(numRows, function(err, rows) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            reject(err)
            } else if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here

            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            resolve(rows)
            })
            })
            }





            share|improve this answer





















            • Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
              – Bergi
              Nov 18 at 15:23










            • @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
              – viswanath
              Nov 19 at 10:24













            up vote
            0
            down vote










            up vote
            0
            down vote









            function fetchRowsFromRS(connection, resultSet, numRows) {
            return resultSet
            .getRows(numRows)
            .then(function(rows) {
            if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here
            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            return rows
            })
            .catch(function(err) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            }
            })
            }


            The above will work if, resultSet.getRows(numRows) returns a promise instead of accepting a callback or there is some alternate for resultSet.getRows(numRows) which return a promise.




            await fetchRowsFromRS(connection, resultSet, numRows)




            will work only if fetchRowsFromRS(connection, resultSet, numRows) returns a promise, which in turn requires everything defines inside the function to return a promise.



            OR



            Use new Promise



            function fetchRowsFromRS(connection, resultSet, numRows) {
            return new Promise(function(resolve, reject) {
            resultSet.getRows(numRows, function(err, rows) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            reject(err)
            } else if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here

            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            resolve(rows)
            })
            })
            }





            share|improve this answer












            function fetchRowsFromRS(connection, resultSet, numRows) {
            return resultSet
            .getRows(numRows)
            .then(function(rows) {
            if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here
            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            return rows
            })
            .catch(function(err) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            }
            })
            }


            The above will work if, resultSet.getRows(numRows) returns a promise instead of accepting a callback or there is some alternate for resultSet.getRows(numRows) which return a promise.




            await fetchRowsFromRS(connection, resultSet, numRows)




            will work only if fetchRowsFromRS(connection, resultSet, numRows) returns a promise, which in turn requires everything defines inside the function to return a promise.



            OR



            Use new Promise



            function fetchRowsFromRS(connection, resultSet, numRows) {
            return new Promise(function(resolve, reject) {
            resultSet.getRows(numRows, function(err, rows) {
            if (err) {
            console.error(err)
            doClose(connection, resultSet) // always close the ResultSet
            reject(err)
            } else if (rows.length > 0) {
            console.log('fetchRowsFromRS(): Got ' + rows.length + ' rows')
            console.log(rows) //getting data here

            if (rows.length === numRows)
            // might be more rows
            fetchRowsFromRS(connection, resultSet, numRows)
            else doClose(connection, resultSet) // always close the ResultSet
            } else {
            // no rows
            doClose(connection, resultSet) // always close the ResultSet
            }
            resolve(rows)
            })
            })
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 18 at 15:12









            Jeevan

            1326




            1326












            • Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
              – Bergi
              Nov 18 at 15:23










            • @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
              – viswanath
              Nov 19 at 10:24


















            • Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
              – Bergi
              Nov 18 at 15:23










            • @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
              – viswanath
              Nov 19 at 10:24
















            Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
            – Bergi
            Nov 18 at 15:23




            Avoid the Promise constructor antipattern! And in your first snippet, make sure to return the inner promises from the then callback or they won't be awaited.
            – Bergi
            Nov 18 at 15:23












            @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
            – viswanath
            Nov 19 at 10:24




            @Jeevan Thanks a lot! for you suggestion.I've used resultSet.getRows(numRows) it returns a promise.
            – viswanath
            Nov 19 at 10:24










            viswanath is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            viswanath is a new contributor. Be nice, and check out our Code of Conduct.













            viswanath is a new contributor. Be nice, and check out our Code of Conduct.












            viswanath is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361690%2fcorrect-way-of-using-async-await-in-node-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno