I came across the following code while working with promises. And it works correctly. I have read shallowly on how async/await code is run on node. But, in the following code, how is the session variable accessible inside the .then() function? Is it just by sheer chance that this code works or is there something about how node runs async/await
Tag: async-await
How to retain values of array inside async loop? javascript
I have code below that uses api to fetch customer data. Problem is when it the loop went to the second index, the customerIds does retain the value from the previous index (see console log below). Somebody knows how to achieve this properly? Here is my code Console logs: The problem can be shown by the text being printed. As
what’s the difference between using then in argument and not
what’s the difference between these two promises one is used in argument other outisde , which one is preferred Answer This is probably opinion based. I think the first one is preferred because you won’t end up with nested promises and should be easier to read. To make it more obvious: vs The number of indentation of the second way
Why function is executed although await is used?
I have used await keyword in the main function to wait for the completion of async function call to poll() and yet the function call to my_plot is made before the completion of the poll() function. Code output: Expected: Answer Don’t use setTimeout directly from within an async function. Instead, use a Promise-based wrapper. It’s surprising that modern ECMAScript doesn’t
Can I rethrow a rejected await function, and catch it immediately
I’d like to catch all my exceptions in one place, but I can’t do that currently: There is an important thing to note if you like more try/catch. The following code won’t catch the error: […] Remember: a rejected Promise will propagate up in the stack unless you catch it. To catch the error properly in try/catch you would refactor
Global memoizing fetch() to prevent multiple of the same request
I have an SPA and for technical reasons I have different elements potentially firing the same fetch() call pretty much at the same time.[1] Rather than going insane trying to prevent multiple unrelated elements to orchestrate loading of elements, I am thinking about creating a gloabalFetch() call where: the init argument is serialised (along with the resource parameter) and used
async and await in angular api response
I am trying to do task step by step. I have a for loop in a method: for each step must do() task. I want to wait to get response and after response coming go to next i But not work console.log print: Note time to receive response from api is not fix. Any help? Answer You can return a
How to generate a zip file synchronously using JSZip?
I’m working on a React.js application, i have a loop from j=1 to j=2000 and i want to download a zip file when j=1 or j=2000. The problem is that the two zip files downloaded at the same time after the end of the loop. in other words the download of the two zip starts when j = 2000. I
Promise JavaScript Returning Empty Array
createFolder() function is returning an empty array. I am not sure what I am doing wrong but it needs to return the items within project_array Answer This is a classic, what you are doing is resolving the promise with the empty array before your node fs async methods have resolved. Try this instead: In essence, wrap every fs. call in
How to log return value after async/await?
Below have I pasted in PoC code, where I have removed lots of lines, but it shows the problem/question I am facing. createPost() returns a “post number” in the ret variable. I don’t need the “post number for anything else than logging it. With the current implementation, I have to define ret outside of the while loop, and since sequential