Skip to content

Commit 1d30882

Browse files
committed
examples: Convert .then to async/await.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 530dc05 commit 1d30882

20 files changed

+390
-450
lines changed

README.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ const config = {
1616
realm: process.env.ZULIP_REALM,
1717
};
1818

19-
zulip(config).then((zulip) => {
19+
(async () => {
20+
const zulip = await zulip(config);
2021
// The zulip object now initialized with config
21-
zulip.streams.subscriptions.retrieve().then((res) => {
22-
console.log(res);
23-
});
24-
});
22+
console.log(await zulip.streams.subscriptions.retrieve());
23+
})();
2524
```
2625

2726
### With Username & Password
2827

29-
You will need to first retrieve the API key by calling `zulip(config)` and then use the zulip object that it passes to `.then()`
28+
You will need to first retrieve the API key by calling `await zulip(config)`.
3029

3130
```js
3231
const zulip = require('zulip-js');
@@ -36,13 +35,12 @@ const config = {
3635
realm: process.env.ZULIP_REALM,
3736
};
3837

39-
//Fetch API Key
40-
zulip(config).then((zulip) => {
38+
(async () => {
39+
// Fetch API Key
40+
const zulip = await zulip(config);
4141
// The zulip object now contains the API Key
42-
zulip.streams.subscriptions.retrieve().then((res) => {
43-
console.log(res);
44-
});
45-
});
42+
console.log(await zulip.streams.subscriptions.retrieve());
43+
})();
4644
```
4745

4846
### With zuliprc
@@ -56,18 +54,17 @@ key=wlueAg7cQXqKpUgIaPP3dmF4vibZXal7
5654
site=http://localhost:9991
5755
```
5856

59-
Please remember to add this file to your `.gitignore`! Calling `zulip({ zuliprc: 'zuliprc' } )` will read this file and then pass a configured zulip object to `.then()`.
57+
Please remember to add this file to your `.gitignore`! Calling `await zulip({ zuliprc: 'zuliprc' })` will read this file.
6058

6159
```js
6260
const zulip = require('zulip-js');
6361
const path = require('path');
6462
const zuliprc = path.resolve(__dirname, 'zuliprc');
65-
zulip({ zuliprc }).then((zulip) => {
63+
(async () => {
64+
const zulip = await zulip({ zuliprc });
6665
// The zulip object now contains the config from the zuliprc file
67-
zulip.streams.subscriptions.retrieve().then((res) => {
68-
console.log(res);
69-
});
70-
});
66+
console.log(await zulip.streams.subscriptions.retrieve());
67+
})();
7168
```
7269

7370
## Examples
@@ -96,7 +93,7 @@ const params = {
9693
content: 'Something is horribly wrong....',
9794
};
9895

99-
zulip.callEndpoint('/messages', 'POST', params);
96+
await zulip.callEndpoint('/messages', 'POST', params);
10097
```
10198

10299
| Function to call | API Endpoint | Documentation |

examples/accounts.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const zulip = require('../lib');
22

3-
zulip({
4-
username: process.env.ZULIP_USERNAME,
5-
apiKey: process.env.ZULIP_API_KEY,
6-
realm: process.env.ZULIP_REALM,
7-
})
8-
.then((z) => z.accounts.retrieve())
9-
.then(console.log);
3+
process.on('unhandledRejection', (err) => {
4+
console.error(err);
5+
process.exit(1);
6+
});
7+
8+
(async () => {
9+
const z = await zulip({
10+
username: process.env.ZULIP_USERNAME,
11+
apiKey: process.env.ZULIP_API_KEY,
12+
realm: process.env.ZULIP_REALM,
13+
});
14+
console.log(await z.accounts.retrieve());
15+
})();

examples/call_endpoint.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ const config = {
66
realm: process.env.ZULIP_REALM,
77
};
88

9-
const sendParams = {
10-
to: 'bot testing',
11-
type: 'stream',
12-
subject: 'Testing zulip-js',
13-
content: 'Something is horribly wrong....',
14-
};
15-
16-
const z = zulip(config);
9+
(async () => {
10+
const z = await zulip(config);
1711

18-
z.callEndpoint('/messages', 'POST', sendParams);
12+
console.log(
13+
await z.callEndpoint('/messages', 'POST', {
14+
to: 'bot testing',
15+
type: 'stream',
16+
subject: 'Testing zulip-js',
17+
content: 'Something is horribly wrong....',
18+
})
19+
);
20+
})();

examples/emojis.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const config = {
1717
// },
1818
// ...
1919

20-
zulip(config)
21-
.then((z) => z.emojis.retrieve())
22-
.then(console.log)
23-
.catch((err) => console.log(err.msg));
20+
(async () => {
21+
const z = await zulip(config);
22+
console.log(await z.emojis.retrieve());
23+
})();

examples/events.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ const config = {
66
realm: process.env.ZULIP_REALM,
77
};
88

9-
zulip(config)
10-
.then((z) => {
11-
// Retrieve events from a queue, blocking until there is an event (or the request timesout)
12-
const params = {
9+
(async () => {
10+
const z = await zulip(config);
11+
// Retrieve events from a queue, blocking until there is an event (or the request timesout)
12+
console.log(
13+
await z.events.retrieve({
1314
queue_id: process.env.ZULIP_QUEUE_ID,
1415
last_event_id: -1,
1516
dont_block: false,
16-
};
17-
return z.events.retrieve(params).then(console.log);
18-
// Prints
19-
// { msg: '',
20-
// result: 'success',
21-
// handler_id: 2005928,
22-
// events:
23-
// [ { flags: [Object], message: [Object], type: 'message', id: 0 },
24-
// { type: 'heartbeat', id: 1 },
25-
// { flags: [], message: [Object], type: 'message', id: 2 },
26-
// { flags: [], message: [Object], type: 'message', id: 3 },
27-
// { flags: [], message: [Object], type: 'message', id: 4 } ] }
28-
})
29-
.catch((err) => console.log(err.message));
17+
})
18+
);
19+
// Prints
20+
// { msg: '',
21+
// result: 'success',
22+
// handler_id: 2005928,
23+
// events:
24+
// [ { flags: [Object], message: [Object], type: 'message', id: 0 },
25+
// { type: 'heartbeat', id: 1 },
26+
// { flags: [], message: [Object], type: 'message', id: 2 },
27+
// { flags: [], message: [Object], type: 'message', id: 3 },
28+
// { flags: [], message: [Object], type: 'message', id: 4 } ] }
29+
})();

examples/filters.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const config = {
66
realm: process.env.ZULIP_REALM,
77
};
88

9-
zulip(config)
10-
.then((z) => {
11-
z.filters.retrieve().then(console.log);
12-
})
13-
.catch((err) => console.log(err.message));
9+
(async () => {
10+
const z = await zulip(config);
11+
console.log(await z.filters.retrieve());
12+
})();

examples/init.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
const path = require('path');
22
const zulip = require('../lib');
33

4-
const config = {
5-
username: process.env.ZULIP_USERNAME,
6-
apiKey: process.env.ZULIP_API_KEY,
7-
realm: process.env.ZULIP_REALM,
8-
};
4+
(async () => {
5+
// Initialization with zuliprc
6+
const zuliprc = path.resolve(__dirname, 'zuliprc');
7+
let z = await zulip({ zuliprc });
8+
// The zulip object now contains the API key
9+
console.log(z.config);
10+
console.log(await z.streams.subscriptions.retrieve());
911

10-
// Initialization with zuliprc
11-
const zuliprc = path.resolve(__dirname, 'zuliprc');
12-
zulip({ zuliprc })
13-
.then((z) => {
14-
// The zulip object now contains the API key
15-
console.log(z.config);
16-
return z.streams.subscriptions();
17-
})
18-
.then(console.log)
19-
.catch((err) => console.log(err.message));
20-
21-
// Initialization with username & API key
22-
zulip(config)
23-
.then((z) => {
24-
// The zulip object now contains the API key
25-
console.log(z.config);
26-
return z.config.apiKey;
27-
})
28-
.then((key) => {
29-
// Initialization with API key
30-
config.apiKey = key;
31-
return zulip(config).streams.subscriptions();
32-
})
33-
.then(console.log)
34-
.catch((err) => console.log(err.message));
12+
// Initialization with username & API key
13+
const config = {
14+
username: process.env.ZULIP_USERNAME,
15+
apiKey: process.env.ZULIP_API_KEY,
16+
realm: process.env.ZULIP_REALM,
17+
};
18+
z = await zulip(config);
19+
// The zulip object now contains the API key
20+
console.log(z.config);
21+
const key = z.config.apiKey;
22+
// Initialization with API key
23+
config.apiKey = key;
24+
z = await zulip(config);
25+
console.log(await z.streams.subscriptions.retrieve());
26+
})();

examples/interactive_call_endpoint.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const zuliprc = process.argv[5]
1919
? path.resolve(__dirname, process.argv[5])
2020
: path.resolve(homedir, '.zuliprc');
2121

22-
zulip({ zuliprc })
23-
.then((z) => z.callEndpoint(endpoint, method, params))
24-
.then(console.log)
25-
.catch((err) => console.log(err.message));
22+
(async () => {
23+
const z = await zulip({ zuliprc });
24+
console.log(await z.callEndpoint(endpoint, method, params));
25+
})();

0 commit comments

Comments
 (0)