Skip to content

Commit

Permalink
Merge pull request #52 from tumblr/kmck/promises
Browse files Browse the repository at this point in the history
Add Promise support to requests
  • Loading branch information
kmck authored Jun 30, 2016
2 parents 477600f + 46d4a39 commit b07b46c
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 150 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "eslint:recommended",
"env": {
"node": true
"node": true,
"es6": true
},

"parser": "espree",
Expand Down
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ var client = new tumblr.Client({
});
```

The request methods return `Request` objects by default, but you can have it return `Promise` objects instead, if that's more your thing. Pass `returnPromises: true` in the options to `createClient`:

```js
var tumblr = require('tumblr.js');
var client = tumblr.createClient({
credentials: {
// ...
},
returnPromises: true,
});
```

### In the Browser

Due to CORS restrictions, you're going to have a really hard time using this library in the browser. Although GET endpoints on the Tumblr API support JSONP, this library is not intended for in-browser use. Sorry!
Expand All @@ -73,16 +85,26 @@ client.blogPosts('staff', {type: 'photo'}, function(err, resp) {
});
```

In most cases, since options are optional (heh) they are also an optional
argument, so there is no need to pass an empty object when supplying no options,
like:
In most cases, since options are optional (heh) they are also an optional argument, so there is no need to pass an empty object when supplying no options, like:

```js
client.blogPosts('staff', function(err, resp) {
resp.posts; // now we've got all kinds of posts
});
```

If you're using Promises, use `then` and/or `catch` instead of a callback:

```js
client.blogPosts('staff')
.then(function(resp) {
resp.posts;
})
.catch(function(err) {
// oops
});
```

### User Methods

```js
Expand Down Expand Up @@ -217,7 +239,9 @@ client.addPostMethods({
gulp lint # linter
gulp test # just the tests

# Copyright and license
---

## Copyright and license

Copyright 2013-2016 Tumblr, Inc.

Expand Down
Loading

0 comments on commit b07b46c

Please sign in to comment.