You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -187,6 +188,40 @@ var discovery = new DiscoveryV1({
187
188
});
188
189
```
189
190
191
+
### Callbacks vs Promises
192
+
193
+
All SDK methods are asynchronous, as they are making network requests to Watson services. To handle receiving the data from these requests, the SDK offers support for both Promises and Callback functions. A Promise will be returned by default unless a Callback function is provided.
194
+
195
+
```js
196
+
constdiscovery=newwatson.DiscoveryV1({
197
+
/* iam_apikey, version, url, etc... */
198
+
});
199
+
200
+
// using Promises
201
+
discovery.listEnvironments()
202
+
.then(body=> {
203
+
console.log(JSON.stringify(body, null, 2));
204
+
})
205
+
.catch(err=> {
206
+
console.log(err);
207
+
});
208
+
209
+
// using Promises provides the ability to use async / await
210
+
asyncfunctioncallDiscovery() { // note that callDiscovery also returns a Promise
211
+
constbody=awaitdiscovery.listEnvironments();
212
+
}
213
+
214
+
// using a Callback function
215
+
discovery.listEnvironments((err, res) => {
216
+
if (err) {
217
+
console.log(err);
218
+
} else {
219
+
console.log(JSON.stringify(res, null, 2));
220
+
}
221
+
});
222
+
223
+
```
224
+
190
225
### Sending request headers
191
226
192
227
Custom headers can be passed with any request. Each method has an optional parameter `headers` which can be used to pass in these custom headers, which can override headers that we use as parameters.
@@ -219,6 +254,8 @@ assistant.message({
219
254
220
255
To retrieve the HTTP response, all methods can be called with a callback function with three parameters, with the third being the response. Users for example may retrieve the response headers with this usage pattern.
221
256
257
+
If using Promises, the parameter `return_response` must be added and set to `true`. Then, the result returned will be equivalent to the third argument in the callback function - the entire response.
258
+
222
259
Here is an example of how to access the response headers for Watson Assistant:
Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+13-1Lines changed: 13 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,19 @@
15
15
16
16
## Breaking changes
17
17
### Methods no longer return stream
18
-
Previously, if a callback was not provided, each service method would return a pipeable stream. Now, this will no longer happen. This should not affect many users, as authenticating with IAM already prevented methods from returning streams.
18
+
Previously, if a callback was not provided, each service method would return a pipeable stream. Now, this will no longer happen. A Promise is returned instead. This should not affect many users, as authenticating with IAM already prevented methods from returning streams.
19
+
20
+
### Response Handling Compatibility
21
+
The entire response is received with the third callback (if using callbacks) or when using Promises with `return_response: true` set as a parameter. This response now has a different structure for consistency with the other Watson SDKs.
22
+
23
+
The object returned has the following properties:
24
+
-`data`: The body of the response
25
+
-`headers`: The HTTP response headers
26
+
-`status`: The HTTP status code
27
+
-`statusText`: The HTTP status message
28
+
-`request`: The actual request instance sent to the service
29
+
-`config`: The parameters used to build the request
30
+
19
31
20
32
### Error Handling Compatibility
21
33
The logic that formats errors returned from a service has been changed. That means that the Error object returned now has a different structure. If there is logic in your code that parses the error returned from service methods, this will need to change. The new structure is outlined below.
0 commit comments