2
2
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3
3
4
4
5
- - [ Promise Class 2.0.0-dev1 ] ( #promise-class-200-dev1 )
5
+ - [ Promise Class 2.0.0-rc1 ] ( #promise-class-200-rc1 )
6
6
- [ Usage] ( #usage )
7
7
- [ Promise()] ( #promise )
8
8
- [ .then()] ( #then )
9
9
- [ .fail()] ( #fail )
10
10
- [ .finally()] ( #finally )
11
11
- [ Promise.loop()] ( #promiseloop )
12
12
- [ Promise.serial()] ( #promiseserial )
13
+ - [ Promise.parallel()] ( #promiseparallel )
14
+ - [ Promise.first()] ( #promisefirst )
13
15
- [ Example] ( #example )
14
16
- [ Testing] ( #testing )
15
17
- [ TL;DR] ( #tldr )
23
25
24
26
[ ![ Build Status] ( https://travis-ci.org/electricimp/Promise.svg?branch=develop )] ( https://travis-ci.org/electricimp/Promise )
25
27
26
- # Promise Class 2.0.0-dev1
28
+ # Promise Class 2.0.0-rc1
27
29
28
30
This Promise class is based on the PromiseJS definition at:
29
31
https://www.promisejs.org/implementing/
@@ -40,9 +42,9 @@ with any sort of detectible failure. Usually, an instantiated Promise object is
40
42
returned from a class instead of offering direct callback functions. This uniform
41
43
implementation makes the code clearer and easier to read.
42
44
43
- ** To add this library to your project, add ` #require "promise.class.nut:2.0.0-dev1 " ` to the top of your device code.**
45
+ ** To add this library to your project, add ` #require "promise.class.nut:2.0.0-rc1 " ` to the top of your device code.**
44
46
45
- You can view the library's source code on [ GitHub] ( https://github.com/electricimp/Promise/tree/v2.0.0-dev1 ) .
47
+ You can view the library's source code on [ GitHub] ( https://github.com/electricimp/Promise/tree/v2.0.0-rc1 ) .
46
48
47
49
## Usage
48
50
@@ -72,13 +74,17 @@ This function allows the developer to provide a function that is executed once t
72
74
73
75
### Promise.loop()
74
76
75
- ` Promise.loop(compareFunction , nextFunction) `
77
+ ` Promise.loop(continueFunction , nextFunction) `
76
78
77
79
A way to perform while loops with asynchronous processes.
78
80
79
- Stops on ` compareFunction() == false ` or first rejection of looped _ Promise_ 's.
81
+ Parameters:
82
+ - ` continueFunction ` – function that returns ` true ` to continue loop or ` false ` to stop
83
+ - ` nextFunction ` – function that returns next _ Promise_ in the loop
80
84
81
- Returns _ Promise_ that is resolved/rejected with the last value that come from looped _ Promise_ when loop finishes.
85
+ Stops on ` continueFunction() == false ` or first rejection of looped _ Promise_ 's.
86
+
87
+ Returns _ Promise_ that is resolved/rejected with the last value that comes from looped _ Promise_ when loop finishes.
82
88
83
89
For example in the following code ` p ` resolves with value "counter is 3" in 9 seconds.
84
90
@@ -96,23 +102,71 @@ local p = Promise.loop(
96
102
);
97
103
```
98
104
99
-
100
105
### Promise.serial()
101
106
102
- ` Promise.serial(promises ) `
107
+ ` Promise.serial(series ) `
103
108
104
109
Returns _ Promise_ that resolves when all promises in chain resolve or when the first one rejects.
105
110
106
- For example in the following code ` p ` rejects with value "2" in 2 seconds:
111
+ Parameters:
112
+ - ` series ` – array of _ Promises_ /functions that return promises.
113
+
114
+ For example in the following code ` p ` rejects with value "2" in 2.5 seconds:
107
115
108
116
``` squirrel
109
- local promises = [
117
+ local series = [
110
118
Promise(@(resolve, reject) imp.wakeup(1, @() resolve(1))),
111
- Promise(@(resolve, reject) imp.wakeup(1, @() reject(2))),
112
- Promise(@(resolve, reject) imp.wakeup(1, @() resolve(3)))
119
+ @() Promise(@(resolve, reject) imp.wakeup(1.5, @() reject(2))),
120
+ Promise(@(resolve, reject) imp.wakeup(0.5, @() resolve(3)))
121
+ ];
122
+
123
+ local p = Promise.serial(series);
124
+ ```
125
+
126
+ ### Promise.parallel()
127
+
128
+ ` Promise.parallel(series) `
129
+
130
+ Execute Promises in parallel and resolve when they are all done.
131
+ Returns Promise that resolves with last paralleled Promise value or rejects with first rejected paralleled Promise value.
132
+
133
+ Parameters:
134
+ - ` series ` – array of _ Promises_ /functions that return promises.
135
+
136
+ For example in the following code ` p ` resolves with value "2" in 1.5 seconds:
137
+
138
+ ``` squirrel
139
+ local series = [
140
+ @() Promise(@(resolve, reject) imp.wakeup(1, @() resolve(1))),
141
+ @() Promise(@(resolve, reject) imp.wakeup(1.5, @() resolve(2))),
142
+ Promise(@(resolve, reject) imp.wakeup(0.5, @() resolve(3)))
143
+ ];
144
+
145
+ local p = Promise.parallel(series);
146
+ ```
147
+
148
+ ### Promise.first()
149
+
150
+ ` Promise.first(series) `
151
+
152
+ Execute Promises in parallel and resolve when the first is done.
153
+ Returns Promise that resolves/rejects with the first resolved/rejected Promise value.
154
+
155
+ Parameters:
156
+ - ` series ` – array of _ Promises_ /functions that return promises.
157
+
158
+ For example in the following code ` p ` rejects with value "1" in 1 second:
159
+
160
+ ``` squirrel
161
+ local promises = [
162
+ // rejects first as the other one with 1s timeout
163
+ // starts later from inside .first()
164
+ ::Promise(function (resolve, reject) { imp.wakeup(1, @() reject(1)) }),
165
+ @() ::Promise(function (resolve, reject) { imp.wakeup(1.5, @() resolve(2)) }),
166
+ @() ::Promise(function (resolve, reject) { imp.wakeup(1, @() reject(3)) }),
113
167
];
114
168
115
- local p = Promise.serial(promises );
169
+ local p = Promise.parallel(series );
116
170
```
117
171
118
172
## Example
0 commit comments