Skip to content

Commit ebeb5c4

Browse files
authored
Rewrite Examples section
1 parent 4c4fee2 commit ebeb5c4

1 file changed

Lines changed: 28 additions & 44 deletions

File tree

README.md

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,48 @@ If you prefer [Yarn](https://yarnpkg.com/), you can use that as well:
1616

1717
yarn add shortcuts.js
1818

19-
## Examples
19+
## Usage
2020

21-
Here are some examples so you can quickly get started with this library.
21+
First, you must require this library:
2222

23-
### Promises
24-
25-
This library uses native promises, which can be chained like in this example:
26-
27-
```javascript
23+
```js
2824
const shortcuts = require("shortcuts.js");
25+
```
2926

30-
// Get id from the iCloud link
31-
const id = shortcuts.idFromURL("https://www.icloud.com/shortcuts/903110dea9a944f48fef9e94317fb686");
27+
Afterwards, you can use its methods. One of the simplest things you can do with this library is grab the ID from an iCloud URL. This is useful for later methods such as getting a shortcut's details from a user-submitted value, which might not always be just the ID.
3228

33-
let resolvedShortcut;
34-
// Lookup the shortcut via its ID
35-
shortcuts.getShortcutDetails(id)
36-
.then(shortcut => {
37-
resolvedShortcut = shortcut;
38-
// Pass the metadata promise down the chain
39-
return shortcut.getMetadata();
40-
})
41-
.then(metadata => {
42-
console.log(resolvedShortcut);
43-
console.log(metadata);
44-
})
45-
.catch(err => {
46-
console.log(err);
47-
})
29+
```js
30+
// Get an ID from a iCloud URL
31+
const id = shortcuts.idFromURL("https://www.icloud.com/shortcuts/903110dea9a944f48fef9e94317fb686");
4832
```
4933

50-
### async/await
51-
52-
You can also use async/await functionality:
34+
This example uses promise chaining to get shortcut metadata from an iCloud URL:
5335

54-
```javascript
55-
const shortcuts = require("shortcuts.js");
36+
```
37+
// Chain promises to get a shortcut's metadata
38+
shortcuts.getShortcutDetails(id).then(shortcut => {
39+
console.log(`Hi, I'm ${id}! What's your name?`);
40+
return shortcut.getMetadata();
41+
}).then(metadata => {
42+
console.log(`I have ${metadata.actions.length} actions! How cool is that?`);
43+
}).catch(error => {
44+
console.log(`${error.code}? How could this happen!`);
45+
});
46+
```
5647

57-
// Get the id from the iCloud link
58-
const id = shortcuts.idFromURL("https://www.icloud.com/shortcuts/903110dea9a944f48fef9e94317fb686");
48+
You can also use [async/await](https://javascript.info/async-await) to simplify things:
5949

60-
async function myAsyncMethod(){
61-
// Lookup the shortcut via its ID
50+
```
51+
async function getBasicInfo() {
6252
const shortcut = await shortcuts.getShortcutDetails(id);
63-
64-
// Lookup the shortcut metadata
6553
const metadata = await shortcut.getMetadata();
66-
67-
console.log(shortcut);
68-
console.log(metadata);
54+
55+
return `Hi! I'm ${shortcut.name}. I have ${metadata.importQuestions.length} import questions, and I'm happy to be here. What's your name?`;
6956
}
7057
71-
try {
72-
myAsyncMethod();
73-
}
74-
catch(err){
75-
console.log(err);
76-
}
58+
getBasicInfo().then(console.log).catch(error => {
59+
console.log(`There was an error: ${error.code}. At least I can say that I tried...`);
60+
});
7761
```
7862

7963
## Documentation

0 commit comments

Comments
 (0)