-
Notifications
You must be signed in to change notification settings - Fork 3
Async Examples
Patrick Meinecke edited this page Apr 30, 2018
·
1 revision
Interacting with async/await based API's is the primary reason this module was created. Here are some examples to demonstrate how to use this module to accomplish that.
$result = $null
$null = [System.Net.Dns]::BeginResolve(
'localhost',
[psdelegate]{ $ar => { $result = [System.Net.Dns]::EndResolve($ar) }},
$null)
First sets a variable $result
in the current scope. This is done because PSLambda
can access variables from the scope it was created it. It then initiates Dns.BeginResolve
and then returns to the prompt. Once BeginResolve
has been completed, the $result
variable will be populated with the return value of Dns.EndResolve
.
using namespace System.Net
using namespace System.Threading.Tasks
$delegate = New-PSDelegate {
[Task]::Delay(10000).
ContinueWith{ $task => { return 'message' }}.
ContinueWith{ $task => $Host.UI.WriteLine($task.GetAwaiter().GetResult()) }
}
$resultTask = $delegate.Invoke()
Creates a task that completes after 10 seconds. After that task completes, continue it with a task that returns a string, then with a task that writes that string to the console.