This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpromises.js
128 lines (116 loc) · 3.4 KB
/
promises.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* jshint undef: true */
/* globals Promise */
/**
* Example using Promises and parallel Channels
*/
/** Require the module */
var MikroNode = require('../lib/index.js');
/** Get a Connection Object */
var connection = new MikroNode.Connection(process.argv[2], process.argv[3], process.argv[4], {
closeOnDone : true
});
/**
* This class gets instantiated once for each item returned with an object representing
* the item. You could do different things here based on interface type for instance.
*/
function Interface(intf) {
var _this = this;
Object.keys(intf).forEach(function(key) {
_this[key] = intf[key];
});
}
/**
* This class gets instantiated once with the unaltered response. What you do with it is
* up to you.
*/
function Routes(routeArray) {
routeArray = MikroNode.parseItems(routeArray);
this.routes = {};
for (var i = 0; i < routeArray.length; i++) {
this.routes[routeArray[i]['.id']] = routeArray[i];
}
}
/**
* Start the connection and login sequence. When resolved, you'll have an active,
* authenticated connection ready for channel work
*/
var connPromise = connection.getConnectPromise().then(function resolve(conn) {
/**
* Get promises for 2 comands in parallel. They'll be sent to the device in the order
* you specify BUT which one the device fulfills first is up to it. You should not
* depend on the order.
*/
/**
* For the first command, we're specifying an itemClass and itemKey. The result will be
* a generic object wrapper containing Interface objects keyed by the interface name.
*/
var chan1Promise = conn.getCommandPromise('/interface/print', {
itemClass : Interface,
itemKey : 'name'
});
/**
* For the second command, we're specifying an dataClass. The result will be a single
* Routes object containing whatever you populated in the Routes constructor.
*/
var chan2Promise = conn.getCommandPromise('/ip/route/print', {
dataClass : Routes
});
/**
* We're now waiting for BOTH commands to resolve or reject.
*/
Promise.all([ chan1Promise, chan2Promise ]).then(function resolved(values) {
/**
* Since .all was called with [ chan1Promise, chan2Promise ] values[0] will be the
* result from chan1Promise and values[1] will be the result from chan2Promise
* regardless of the order you created the promises.
*/
console.log('Interfaces via Promise: ', values[0]);
console.log('Routes via Promise: ', values[1]);
}, function rejected(reason) {
/**
* This is the rejection from the command promises. You may get Socket errors if the
* connection fails, or a Trap if there was a problem with the command.
*/
console.log('Oops: ' + reason);
});
/**
* There are lots of ways to code this besides the above.
*
* <pre>
* var p = Promise.all([ chan1Promise, chan2Promise ]);
* p.then(function(values){
* ...
* });
* p['catch'](function(result){
* ...
* });
* </pre>
*
* or
*
* <pre>
* Promise.all([ chan1Promise, chan2Promise ]).then(function resolved(values){
* ...
* }).catch(function rejected(result){
* ...
* });
* </pre>
*
* or
*
* <pre>
* Promise.all([ chan1Promise, chan2Promise ], function resolved(values){
* ...
* }, function rejected(result){
* ...
* });
* </pre>
*
*/
}, function reject(result) {
/**
* This is the rejection from the connect promise. You may get Socket errors if the
* connection failed or a Trap if login failed.
*/
console.log(result);
});