forked from Real-Serious-Games/C-Sharp-Promise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromiseHelpers.cs
More file actions
70 lines (64 loc) · 2.38 KB
/
PromiseHelpers.cs
File metadata and controls
70 lines (64 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RSG
{
public static class PromiseHelpers
{
/// <summary>
/// Returns a promise that resolves with all of the specified promises have resolved.
/// Returns a promise of a tuple of the resolved results.
/// </summary>
public static IPromise<Tuple<T1, T2>> All<T1, T2>(IPromise<T1> p1, IPromise<T2> p2)
{
var val1 = default(T1);
var val2 = default(T2);
var numUnresolved = 2;
var promise = new Promise<Tuple<T1, T2>>();
p1
.Then(val =>
{
val1 = val;
numUnresolved--;
if (numUnresolved <= 0)
{
promise.Resolve(Tuple.Create(val1, val2));
}
})
.Catch(e => promise.Reject(e))
.Done();
p2
.Then(val =>
{
val2 = val;
numUnresolved--;
if (numUnresolved <= 0)
{
promise.Resolve(Tuple.Create(val1, val2));
}
})
.Catch(e => promise.Reject(e))
.Done();
return promise;
}
/// <summary>
/// Returns a promise that resolves with all of the specified promises have resolved.
/// Returns a promise of a tuple of the resolved results.
/// </summary>
public static IPromise<Tuple<T1, T2, T3>> All<T1, T2, T3>(IPromise<T1> p1, IPromise<T2> p2, IPromise<T3> p3)
{
return All(All(p1, p2), p3)
.Then(vals => Tuple.Create(vals.Item1.Item1, vals.Item1.Item2, vals.Item2));
}
/// <summary>
/// Returns a promise that resolves with all of the specified promises have resolved.
/// Returns a promise of a tuple of the resolved results.
/// </summary>
public static IPromise<Tuple<T1, T2, T3, T4>> All<T1, T2, T3, T4>(IPromise<T1> p1, IPromise<T2> p2, IPromise<T3> p3, IPromise<T4> p4)
{
return All(All(p1, p2), All(p3, p4))
.Then(vals => Tuple.Create(vals.Item1.Item1, vals.Item1.Item2, vals.Item2.Item1, vals.Item2.Item2));
}
}
}