Skip to content

Commit 7b4e8a3

Browse files
authored
Merge pull request #813 from watson-developer-cloud/add-promise-support
add support for promises
2 parents 07c1c7d + dc08ced commit 7b4e8a3

32 files changed

Lines changed: 8270 additions & 908 deletions

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Node.js client library to use the Watson APIs.
2020
* [IAM](#iam)
2121
* [Username and password](#username-and-password)
2222
* [API key](#api-key)
23+
* [Callbacks vs Promises](#callbacks-vs-promises)
2324
* [Sending request headers](#sending-request-headers)
2425
* [Parsing HTTP response](#parsing-http-response)
2526
* [Data collection opt-out](#data-collection-opt-out)
@@ -187,6 +188,40 @@ var discovery = new DiscoveryV1({
187188
});
188189
```
189190

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+
const discovery = new watson.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+
async function callDiscovery() { // note that callDiscovery also returns a Promise
211+
const body = await discovery.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+
190225
### Sending request headers
191226

192227
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({
219254

220255
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.
221256

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+
222259
Here is an example of how to access the response headers for Watson Assistant:
223260

224261
```js
@@ -233,6 +270,18 @@ assistant.message(params, function(err, result, response) {
233270
console.log(response.headers);
234271
});
235272

273+
// using Promises
274+
275+
params.return_response = true;
276+
277+
assistant.message(params)
278+
.then(response => {
279+
console.log(response.headers);
280+
})
281+
.catch(err => {
282+
console.log('error:', err);
283+
});
284+
236285
```
237286

238287
### Data collection opt-out

UPGRADE-4.0.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515

1616
## Breaking changes
1717
### 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+
1931

2032
### Error Handling Compatibility
2133
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

Comments
 (0)