I’m familiar with Promises, but have inherited some rather unusual code that, rather than making a new Promise(), uses the following: From my research, this is a weird version of setImmediate – ie, run the following function on the next tick. What would be the await version of this? Answer There may be two different reasons for the Promise.resolve(). You
Tag: async-await
Correct Try…Catch Syntax Using Async/Await
I like the flatness of the new Async/Await feature available in Typescript, etc. However, I’m not sure I like the fact that I have to declare the variable I’m awaiting on the outside of a try…catch block in order to use it later. Like so: Please correct me if I’m wrong, but it seems to be best practice not to
When to mark function as async
Basically, function must be prefixed with async keyword if await used inside it. But if some function just returns Promise and doesn’t awaiting for anything, should I mark the function as async? Seems like both correct or not? Answer if some function just returns Promise and doesn’t awaiting for anything, should I mark the function as async? I would say
Is it an anti-pattern to use async/await inside of a new Promise() constructor?
I’m using the async.eachLimit function to control the maximum number of operations at a time. As you can see, I can’t declare the myFunction function as async because I don’t have access to the value inside the second callback of the eachLimit function. Answer You’re effectively using promises inside the promise constructor executor function, so this the Promise constructor anti-pattern.
JavaScript array .reduce with async/await
Seem to be having some issues incorporating async/await with .reduce(), like so: The data object is logged before the this.store completes… I know you can utilise Promise.all with async loops, but does that apply to .reduce()? Answer The problem is that your accumulator values are promises – they’re return values of async functions. To get sequential evaluation (and all but
Chain async functions
In an async function, I can get an asynchronous value like so: const foo = await myAsyncFunction() If I want to call a method on the result, with a sync function I’d do something like myAsyncFunction().somethingElse() Is it possible to chain calls with async functions, or do you have to assign a new variable for each result? Answer I prefer
Is there a way to short circuit async/await flow?
All four functions are called below in update return promises. What if we want to abort the sequence from outside, at any given time? For example, while fetchMetaData is being executed, we realize we no longer need to render the component and we want to cancel the remaining operations (fetchContent and render). Is there a way to abort/cancel these operations
How to use ES8 async/await with streams?
In https://stackoverflow.com/a/18658613/779159 is an example of how to calculate the md5 of a file using the built-in crypto library and streams. But is it possible to convert this to using ES8 async/await instead of using the callback as seen above, but while still keeping the efficiency of using streams? Answer The await keyword only works on promises, not on streams.
Combination of async function + await + setTimeout
I am trying to use the new async features and I hope solving my problem will help others in the future. This is my code which is working: The problem is, that my while loop runs too fast and the script sends too many requests per second to the google API. Therefore I would like to build a sleep function