Last year I blogged about how to introduce a delay between web service requests in M code. Since then a new function has been added to Power Query which makes this a lot easier: Function.InvokeAfter(). This function doesn’t seem to be documented anywhere apart from the Power Query language reference (downloadable from here); the signature is as follows:
Function.InvokeAfter(function as function, delay as duration) as any
It invokes a function after waiting for a given amount of time. Here’s a simple example of how it can be used that declares a function which returns the current date/time as text, then calls it twice with a five second delay in between:
let GetTimeAsText = ()=> DateTime.ToText(DateTime.LocalNow()), Output = GetTimeAsText() & " " & Function.InvokeAfter(GetTimeAsText, #duration(0,0,0,5)) in Output
The output of this query (at the time of writing) is:
28/04/2015 23:06:38 28/04/2015 23:06:43
One thing that did confuse me a bit was the fact that Function.InvokeAfter() doesn’t allow you to pass a list of arguments for the function you’re invoking like Function.Invoke(). The nice people at Microsoft helped me out with this though, and here’s a slightly more complicated example showing how to use Function.InvokeAfter() with a function that appends “Hello “ to a person’s name:
let SayHello = (personname as text) as text => "Hello " & personname, Output = Function.InvokeAfter(()=>SayHello("Chris"), #duration(0,0,0,5)) in Output