Convert promise function to async/awaitIs there an “exists” function for jQuery?How can I convert a string to boolean in JavaScript?var functionName = function() vs function functionName() Set a default parameter value for a JavaScript functionConvert JS object to JSON stringHow and when to use ‘async’ and ‘await’How do I convert an existing callback API to promises?What is the difference between Promises and Observables?Using async/await with a forEach loopis this Promise to async convertion correct and why await is not necessary?

Why am I not getting stuck in the loop

Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?

What is the German idiom or expression for when someone is being hypocritical against their own teachings?

What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?

Not been paid even after reminding the Treasurer; what should I do?

Does a humanoid possessed by a ghost register as undead to a paladin's Divine Sense?

Is a switch from R to Python worth it?

How to switch an 80286 from protected to real mode?

How can I use commands with sudo without changing owner of the files?

Would this winged human/angel be able to fly?

If a vampire drinks blood of a sick human, does the vampire get infected?

Which genus do I use for neutral expressions in German?

Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?

How does LIDAR avoid getting confused in an environment being scanned by hundreds of other LIDAR?

Is there a way to prevent the production team from messing up my paper?

Make a living as a math programming freelancer?

Why does putting a dot after the URL remove login information?

How to sort List<T> in c#

How do I get the =LEFT function in excel, to also take the number zero as the first number?

Why do proponents of guns oppose gun competency tests?

3 beeps on a 486 computer with an American Megatrends bios?

How to check a file was encrypted (really & correctly)

How and where to get you research work assessed for PhD?

Ubuntu show wrong disk sizes, how to solve it?



Convert promise function to async/await


Is there an “exists” function for jQuery?How can I convert a string to boolean in JavaScript?var functionName = function() vs function functionName() Set a default parameter value for a JavaScript functionConvert JS object to JSON stringHow and when to use ‘async’ and ‘await’How do I convert an existing callback API to promises?What is the difference between Promises and Observables?Using async/await with a forEach loopis this Promise to async convertion correct and why await is not necessary?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2















I am trying to convert this function to async/await. But got confused. Is there a way to do it?



const csv = require('fast-csv');
const inputFileFolder = 'upload';

const readData = (inputFileName) =>
const csvData = [];

return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;









share|improve this question





















  • 1





    what's the question?

    – Rafael
    Mar 27 at 3:52






  • 1





    You can't use async/await with that - you need to use it within the function that is handling the promise.

    – Jack Bashford
    Mar 27 at 3:52






  • 2





    I guess you could await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData

    – CertainPerformance
    Mar 27 at 3:54











  • @JackBashford I do it. But was wondering if I can make this async/await.

    – Shaolin
    Mar 27 at 3:56






  • 1





    Why? "making it async/await" you'll still need to "promisify" csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises

    – Jaromanda X
    Mar 27 at 4:00


















-2















I am trying to convert this function to async/await. But got confused. Is there a way to do it?



const csv = require('fast-csv');
const inputFileFolder = 'upload';

const readData = (inputFileName) =>
const csvData = [];

return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;









share|improve this question





















  • 1





    what's the question?

    – Rafael
    Mar 27 at 3:52






  • 1





    You can't use async/await with that - you need to use it within the function that is handling the promise.

    – Jack Bashford
    Mar 27 at 3:52






  • 2





    I guess you could await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData

    – CertainPerformance
    Mar 27 at 3:54











  • @JackBashford I do it. But was wondering if I can make this async/await.

    – Shaolin
    Mar 27 at 3:56






  • 1





    Why? "making it async/await" you'll still need to "promisify" csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises

    – Jaromanda X
    Mar 27 at 4:00














-2












-2








-2








I am trying to convert this function to async/await. But got confused. Is there a way to do it?



const csv = require('fast-csv');
const inputFileFolder = 'upload';

const readData = (inputFileName) =>
const csvData = [];

return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;









share|improve this question
















I am trying to convert this function to async/await. But got confused. Is there a way to do it?



const csv = require('fast-csv');
const inputFileFolder = 'upload';

const readData = (inputFileName) =>
const csvData = [];

return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;






javascript node.js promise async-await






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 3:55







Shaolin

















asked Mar 27 at 3:50









ShaolinShaolin

1,8963 gold badges25 silver badges34 bronze badges




1,8963 gold badges25 silver badges34 bronze badges










  • 1





    what's the question?

    – Rafael
    Mar 27 at 3:52






  • 1





    You can't use async/await with that - you need to use it within the function that is handling the promise.

    – Jack Bashford
    Mar 27 at 3:52






  • 2





    I guess you could await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData

    – CertainPerformance
    Mar 27 at 3:54











  • @JackBashford I do it. But was wondering if I can make this async/await.

    – Shaolin
    Mar 27 at 3:56






  • 1





    Why? "making it async/await" you'll still need to "promisify" csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises

    – Jaromanda X
    Mar 27 at 4:00













  • 1





    what's the question?

    – Rafael
    Mar 27 at 3:52






  • 1





    You can't use async/await with that - you need to use it within the function that is handling the promise.

    – Jack Bashford
    Mar 27 at 3:52






  • 2





    I guess you could await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData

    – CertainPerformance
    Mar 27 at 3:54











  • @JackBashford I do it. But was wondering if I can make this async/await.

    – Shaolin
    Mar 27 at 3:56






  • 1





    Why? "making it async/await" you'll still need to "promisify" csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises

    – Jaromanda X
    Mar 27 at 4:00








1




1





what's the question?

– Rafael
Mar 27 at 3:52





what's the question?

– Rafael
Mar 27 at 3:52




1




1





You can't use async/await with that - you need to use it within the function that is handling the promise.

– Jack Bashford
Mar 27 at 3:52





You can't use async/await with that - you need to use it within the function that is handling the promise.

– Jack Bashford
Mar 27 at 3:52




2




2





I guess you could await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData

– CertainPerformance
Mar 27 at 3:54





I guess you could await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData

– CertainPerformance
Mar 27 at 3:54













@JackBashford I do it. But was wondering if I can make this async/await.

– Shaolin
Mar 27 at 3:56





@JackBashford I do it. But was wondering if I can make this async/await.

– Shaolin
Mar 27 at 3:56




1




1





Why? "making it async/await" you'll still need to "promisify" csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises

– Jaromanda X
Mar 27 at 4:00






Why? "making it async/await" you'll still need to "promisify" csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises

– Jaromanda X
Mar 27 at 4:00













2 Answers
2






active

oldest

votes


















1














async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.



Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.






share|improve this answer


































    -1














    You can only use await if you declare it in a async function.
    You can use it and then use correct error handling.
    But if you want to use await you could use



    async function name() 
    await // Your code here
    ;





    share|improve this answer

























    • I know how to write async function. My problem was converting this particular one.

      – Shaolin
      Mar 27 at 4:21













    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',
    autoActivateHeartbeat: false,
    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369523%2fconvert-promise-function-to-async-await%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









    1














    async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.



    Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.






    share|improve this answer































      1














      async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.



      Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.






      share|improve this answer





























        1












        1








        1







        async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.



        Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.






        share|improve this answer















        async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.



        Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 27 at 7:22

























        answered Mar 27 at 7:17









        Estus FlaskEstus Flask

        86.8k25 gold badges143 silver badges262 bronze badges




        86.8k25 gold badges143 silver badges262 bronze badges


























            -1














            You can only use await if you declare it in a async function.
            You can use it and then use correct error handling.
            But if you want to use await you could use



            async function name() 
            await // Your code here
            ;





            share|improve this answer

























            • I know how to write async function. My problem was converting this particular one.

              – Shaolin
              Mar 27 at 4:21















            -1














            You can only use await if you declare it in a async function.
            You can use it and then use correct error handling.
            But if you want to use await you could use



            async function name() 
            await // Your code here
            ;





            share|improve this answer

























            • I know how to write async function. My problem was converting this particular one.

              – Shaolin
              Mar 27 at 4:21













            -1












            -1








            -1







            You can only use await if you declare it in a async function.
            You can use it and then use correct error handling.
            But if you want to use await you could use



            async function name() 
            await // Your code here
            ;





            share|improve this answer













            You can only use await if you declare it in a async function.
            You can use it and then use correct error handling.
            But if you want to use await you could use



            async function name() 
            await // Your code here
            ;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 4:08









            GyzimoGyzimo

            397 bronze badges




            397 bronze badges















            • I know how to write async function. My problem was converting this particular one.

              – Shaolin
              Mar 27 at 4:21

















            • I know how to write async function. My problem was converting this particular one.

              – Shaolin
              Mar 27 at 4:21
















            I know how to write async function. My problem was converting this particular one.

            – Shaolin
            Mar 27 at 4:21





            I know how to write async function. My problem was converting this particular one.

            – Shaolin
            Mar 27 at 4:21

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369523%2fconvert-promise-function-to-async-await%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

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh