Skip to content

Commit 0d37615

Browse files
committed
Passes result value as custom error property if timeout
Fixes #3.
1 parent d9d3705 commit 0d37615

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ RED.start().then(embeddedStart(RED)).then((result) => {
5858
// result is whatever RED.start() resolved to
5959
// RED.node.getFlow() etc are now ready to use
6060
}).catch((err) => {
61-
// theoretically, RED.start() could have rejected, but in practice
62-
// this isn't possible (at present) given the Node-RED code
63-
// so in practice embeddedStart() timed out (see below)
61+
if (/^timed out/.test(err.message)) {
62+
// embeddedStart() timed out
63+
// the value that RED.start() resolved to is available as err.result
64+
}
6465
});
6566
```
6667

@@ -74,9 +75,22 @@ embeddedStart.inject(RED);
7475
// then use RED.start() just as you would normally
7576
RED.start().then((result) => {
7677
// RED.node.getFlow() etc are now ready to use
78+
}).catch((err) => {
79+
// same as above example
7780
});
7881
```
7982

83+
In either case, the promise returned by the function settles in one of the
84+
following three ways (the "result value" is optional and should normally be
85+
the value to which `RED.start()` resolves):
86+
1. If the flows API is ready already, the promise resolves immediately with
87+
the result value passed in.
88+
2. When the `nodes-started` event fires, the promise resolves with the result
89+
value passed in.
90+
3. If the timeout is reached before the `nodes-started` event fires, the
91+
promise rejects with an error. If a result value different from `undefined`
92+
is passed in, it is passed through as the `result` property of the error.
93+
8094
### Usage & API details
8195

8296
#### Generate a wait function: `embeddedStart([RED[, timeout]])`

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function waitNodesStarted(RED, timeout, result) {
1111
// timeout with failure
1212
let timer = setTimeout(() => {
1313
events.removeListener('nodes-started', nodesStarted);
14-
reject(new Error("timed out waiting for flows to start"));
14+
let err = new Error("timed out waiting for flows to start");
15+
if (result !== undefined) err.result = result;
16+
reject(err);
1517
}, timeout);
1618

1719
// handler for 'nodes-started' event

0 commit comments

Comments
 (0)