Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

amei's avatar
amei
Explorer | Level 3
3 months ago

Returning array from downloaded dropbox file in async function

One more issue I am trying to resolve. As I mentioned previously I was able to download the file from dropbox and properly parse it into an array and access array elements within the async function I use to download the dropbox file. However I am having issues using the array outside of the async function LoadFileFromDropBox. It looks like it returns a promise not an array I can use outside the function. I have tried a few things get the array from the returned promise but unsuccessful. Here is the functions I am trying to work with :

I have tried various calls to try to access the array outside the async function :

The usual results I get on the console is as follows :

Any assistance to be able to use the users_array outside of the async functions would be greatly appreciated. Thanks. 

7 Replies

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    3 months ago

    Hi amei​ 

    While this particular issue isn’t related to Dropbox or the Dropbox API, we’re happy to do our best to take a look and help you troubleshoot the code.

    To better understand what’s happening and try some testing on our end, could you please share the full code snippet you’re using?

    Once we have the complete context, we’ll review it and see if we can point you in the right direction.

  • amei's avatar
    amei
    Explorer | Level 3
    3 months ago

    I would you like to send you an attachment of the html code, but I don't see an option for that. There are 2400 lines of code. I could paste it in. 

  • Здравко's avatar
    Здравко
    Legendary | Level 20
    3 months ago

    Hi amei​,

    Hm..🤔 What would be if you change your code to something like:

    Get_Arrays_2().then((users_array) => console.log('Returned Users Array Global :', users_array, '\nUser1 Week1 Gm1 Pick Global :', users_array[1][1][1]));

    Do "usual results" change? 🙃 Take a look here for more info.

    Good luck! 😉

  • amei's avatar
    amei
    Explorer | Level 3
    3 months ago

    The line you mentioned above does give the desired results when used within the then syntax. But when I try to use it outside the then syntax (a couple of lines further down) users_array is not defined. 

            Get_Arrays_2().then((users_array) => 
                console.log('Returned Users Array Global :', users_array, '\nUser1 Week1 Gm1 Pick Global :', users_array[1][1][1]));

    //        var users_array = Promise.resolve(users_array_promise);
            console.log('Returned Users Array Global : ', users_array);
            console.log('User1 Week1 Gm1 Pick Global : ', users_array[1][1][1]);

    Results :

     

  • Здравко's avatar
    Здравко
    Legendary | Level 20
    3 months ago

    Hm.. 🤔  Ok, amei​ , can you point me to any programing language (not only Java Script) where you can use some function parameter outside the function body (where the param has been declared)? 🧐🤷

    Even more, can you use something (either declared or not) before it has been defined (again on any programing language)? Note the messages sequence in your terminal output and when the error appears (either on your original post or the last one) - before the place where the promis has been fulfilled (i.e. where becomes defined) or after that! 🙂 One more thing: Did you notice that in your OP the promise is pending and fulfilled at the same time?! Is this possible or... What? Think a bit. 😉

    Hope this gives direction.

  • amei's avatar
    amei
    Explorer | Level 3
    3 months ago

    Functions can normally can return a value that can be used outside of the function with the return keyword in the function (example in my code below). The issue is with async functions as they return promises and not variables (e.g. string, int, array). With your example using then I tried to define the array inside the then bracket but was unsuccessful. I have not worked with async functions before so I am having issues with getting the users_array from the downloaded file to a global array that I can use within the rest of the program. Works fine inside the function and then statement. 

    Non async function returns Upset class as a global variable. No promise to deal with in non-async function. 

            function upset_classes(ps){
                if(ps >= -3.0) {
                    upset_class = "Small"
                } else if(ps < -3.0 && ps > -7.0) {
                    upset_class = "Medium"
                } else {
                    upset_class = "Large"
                }
            return upset_class;
            }

    My console outputs sections are as follows (see console image previous reply):

    Lines 1893-1895 - using async functions Get_Arrays2() and LoadFileFromDropbox()

    Line 1970 - from your line using then statement

    Lines 1973 and 1974 - These lines are trying to use users array outside of functions, once the functions are called. 

    As you mentioned I will try to get more detail about the status of the promise. I believe if I can sort that out I should be able to return the array. 

    thanks for you input so far !!

  • Здравко's avatar
    Здравко
    Legendary | Level 20
    3 months ago

    In fact, you almost give answer to your question. Yes, that's the difference and I tried to point you to. I supposed that you have no experience with async constructions (including functions and promises) and that's why I posted a link to such a resource where you may read more, but - as it seems - you have ignored. Read such tutorials and so and get familiar with async coding!

    In short - ASYNC means that code execution continue without wait function to finish and that's why it's not mandatory the response to be available exactly after the function! That's why code has to wait for the result somehow - using 'then' or 'await' - if needed. Such a wait is not mandatory if your code structure is well designed actually.

    With code of "Hello world" complexity will be complicated to understand such constructions, so... read! Start with the link I posted above.

About Discuss Dropbox Developer and API

Node avatar for Discuss Dropbox Developer and API
Explore Dropbox Developer & API discussions with developers like you.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!