Posts

Showing posts with the label Asynchronous

Asynchronously Delay JS Until A Condition Is Met

Answer : Consider this: (function wait() { if ( chatroom.json ) { chatroom.render(); } else { setTimeout( wait, 500 ); } })(); This will check every half second. Live demo: http://jsfiddle.net/kBgTx/

Asynchronous Shell Exec In PHP

Answer : If it "doesn't care about the output", couldn't the exec to the script be called with the & to background the process? EDIT - incorporating what @AdamTheHut commented to this post, you can add this to a call to exec : " > /dev/null 2>/dev/null &" That will redirect both stdio (first > ) and stderr ( 2> ) to /dev/null and run in the background. There are other ways to do the same thing, but this is the simplest to read. An alternative to the above double-redirect: " &> /dev/null &" I used at for this, as it is really starting an independent process. <?php `echo "the command"|at now`; ?> To all Windows users: I found a good way to run an asynchronous PHP script (actually it works with almost everything). It's based on popen() and pclose() commands. And works well both on Windows and Unix. function execInBackground($cmd) { if (substr(php_uname(), 0, 7) =...

Callout Limits For Future Methods And Queueable Apex

Image
Answer : I quickly wrote a class with future method to test this behaviour. public class FutureClassLimitsTest { @future(callout=true) public static void docallouts(){ for(Integer i=0;i<200;i++){ Http http=new Http(); HttpRequest hr=new HttpRequest(); hr.setMethod('POST'); hr.setEndpoint('https://google.com'); http.send(hr); } } } This is the error I get. First error: Too many callouts: 101. So I believe this is uniform for the whole platform irrespective of the transaction(Sync or Async). Is there a workaround? Well unless there is a strong business requirement it is kinda bad doing so many callous in a transaction. We can call a Queauble Method from Queueable method,.. and you can chain them infinitely. Thus using proper implementation you can have 100+ callouts. Batch is another way to tackle this. You have to write /modify data model add a some queue cus...

Angular / TypeScript - Call A Function After Another One Has Been Completed

Answer : So remove the setTimeout part. It will call resolve or reject and then pass the execution to the next then or catch handler. If you have some asynchronous call in the Promise, you need to call resolve/reject in the result of that call. What about not waiting 1500ms - the given time is actually the lowest time after which the function may be called. Maybe after 2000ms This is related to the main thread in which JS code works. If main thread has no work to done, then the results of the asynchronous calls are going to be executed. function f1() { return new Promise((resolve, reject) => { console.log('f1'); resolve(); }); } function f2() { console.log('f2'); } f1().then(res => f2()); If f1 is synchronous , there is nothing special to do: global() { f1(); f2(); } If f1 is asynchronous and return an Observable , use Rxjs operator , like concatMap: global() { f1().concatMap(() =>...

Await Task.WhenAll(tasks) Exception Handling, Log All Exceptions From The Tasks

Answer : You've fallen foul of lazy evaluation - the result of Select will create a new set of tasks each time you iterate over it. You can fix this just by calling ToList() : var tasks = _factory.CreateMessage(settings) .Select(msg => SendScans(msg)) .ToList(); That way the set of tasks that you're awaiting will be the same set of tasks checked with your foreach loop. Instead of iterating over all tasks, you can get the Exceptions (if any) from the Task.WhenAll -Task: var taskResult = Task.WhenAll(tasks); try { await taskResult; } catch (Exception e) { if (taskResult.IsCanceled) { // Cancellation is most likely due to a shared cancellation token. Handle as needed, possibly check if ((TaskCanceledException)e).CancellationToken == token etc. } else if (taskResult.IsFaulted) { // use taskResult.Exception which is an AggregateException - which you can iterate over (it's a tree...

Async/await In Angular `ngOnInit`

Answer : It is no different than what you had before. ngOnInit will return a Promise and the caller will ignore that promise. This means that the caller will not wait for everything in your method to finish before it proceeds. In this specific case it means the view will finish being configured and the view may be launched before this.data is set. That is the same situation you had before. The caller would not wait for your subscriptions to finish and would possibly launch the app before this.data had been populated. If your view is relying on data then you likely have some kind of ngIf setup to prevent you from accessing it. I personally don't see it as awkward or a bad practice as long as you're aware of the implications. However, the ngIf can be tedious (they would be needed in either way). I have personally moved to using route resolvers where it makes sense so I can avoid this situation. The data is loaded before the route finishes navigating and I can...