Your workflow is unique 👨💻 - tell us how you use Dropbox here.
Forum Discussion
amei
3 months agoExplorer | Level 3
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...
Здравко
3 months agoLegendary | Level 20
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
3 months agoExplorer | Level 3
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 :
- Здравко3 months agoLegendary | Level 20
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.
- amei3 months agoExplorer | Level 3
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 !!
- Здравко3 months agoLegendary | Level 20
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
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!