Skip to content

Commit f5367ef

Browse files
committed
wait and waitAsync
1 parent 8c77c34 commit f5367ef

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

content/developer/script-task-api.mdx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,36 @@ console.error('Hello PROCEED - error()');
188188
console.timeEnd('L1');
189189
```
190190

191-
### `wait( <number in milliseconds> )`
191+
### `wait( <ms> )` [#wait]
192192

193-
An timeout function which stops the Script execution.
194-
It is similar to the conventional _setTimeout()_, which is not available in a Script Task.
193+
A timeout function which pauses/postpones the script execution.
194+
It is synchronous and blocks the execution until the timeout is passed.
195+
_Hint:_ the popular `setTimeout()` is not available in a Script Task.
195196

196-
**OPEN TODO:** The `wait`function returns an integer: it returns `1` if the process was paused (externally by the MS) during the process execution. Else it returns `0`.
197+
If the _entire process_ is paused from the Management System, all script code is also paused until the process is resumed.
198+
If a _pause_ occurs during a `wait()`, the system stores the original expiration time of `wait()`.
199+
If the process resumes before this original expiration time, the script waits for the remaining time before continuing with the next instructions.
200+
If the original expiration time has passed after the process resumes, it immediately continues with the next instructions after `wait()`.
201+
202+
{/* -> uncomment when Bug is solved
203+
**OPEN Bug:** The `wait`function returns an integer: it returns `1` if the process was paused (externally by the MS) during the process execution. Else it returns `0`.
197204
This information is useful since a pausing of the process has an undefined timespan, so the defined _waiting_ time is not reliable anymore.
205+
*/}
198206

199207
| API | |
200208
| -------------------------------- | ------------------------------------------------- |
201-
| `wait(<number in milliseconds>)` | timeout for pausing the the Script Task execution |
209+
| `wait(<milliseconds: number>)` | `milliseconds` for pausing the the Script Task execution |
202210

203211
**Example Code**
204212

213+
```js
214+
// log with time:
215+
log.info("Before wait()");
216+
wait(10000); // 10s
217+
log.info("After wait()");
218+
```
219+
220+
{/* -> uncomment when Bug is solved
205221
```js
206222
let hasBeenPaused = wait(10000);
207223
if (hasBeenPaused) {
@@ -212,6 +228,29 @@ if (hasBeenPaused) {
212228
log.info('sucessfully waited ');
213229
}
214230
```
231+
*/}
232+
233+
### `waitAsync( <ms> ): Promise` [#wait]
234+
235+
<Callout type='warning'>
236+
The use of `waitAsync( <ms> ): Promise` is only suitable for advanced users.
237+
It makes code execution asynchronous.
238+
This allows you to achieve multiple semi-parallel code executions.
239+
</Callout>
240+
241+
Like [`wait()`](#wait) but asynchronous, so it immediately continues with the next code instructions and does not block the specified timeout is finished.
242+
In this sense, it is similar to the usual `setTimeout()` but only with a Promise. So, you can use `await` or `then()`.
243+
Pausing behavior is the same as for `wait()`.
244+
245+
| API | |
246+
| -------------------------------- | ------------------------------------------------- |
247+
| `waitAsync(<milliseconds: number>): Promise` | `milliseconds` for pausing the the Script Task execution. Returns a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). |
248+
249+
**Example Code**
250+
251+
```js
252+
waitAsync(10000).then( () => console.info("Finished waiting 10s") );
253+
```
215254

216255
### `setInterval( <clb>, <ms> )` and `clearInterval( <id> )`
217256

0 commit comments

Comments
 (0)