Skip to content

Commit

Permalink
Merge pull request #8 from vizeat/feat/promise
Browse files Browse the repository at this point in the history
Proper Promise Implementation
  • Loading branch information
Guillaume Badi committed Apr 26, 2016
2 parents b25a903 + c4caceb commit b6a72f6
Show file tree
Hide file tree
Showing 5 changed files with 601 additions and 583 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ user.request(function (error, response, body) {

### Make the same request with a Promise

the request method actually returns a [EventEmitter][eventemitter] triggering `success` and `error`

``` javascript

user.request()
.on('error', function (error, response) {})
.on('success', function (response, body) {});
.then(function (result) {
// do something with the result
// result structure is {response: {...}, body: {...}}
})
.catch(function (reason) {
// handle the rejection reason
// reason structure is {error: {...}, response: {...}}
})

```

Expand All @@ -109,8 +113,8 @@ user.request()
``` javascript

sender.request({ Email: '[email protected]' })
.on('success', handleData)
.on('error', handleError);
.then(handleData)
.catch(handleError);

```

Expand Down Expand Up @@ -166,8 +170,8 @@ var emailData = {
sendEmail
.request(emailData)
.on('success', handlePostResponse)
.on('error', handleError);
.then(handlePostResponse)
.catch(handleError);
```

Expand All @@ -188,13 +192,13 @@ emailData2['Text-part'] = 'This is another Email';
sendEmail
.request(emailData)
.on('success', handleData)
.on('error', handleError);
.then(handleData)
.catch(handleError);
sendEmail
.request(emailData2)
.on('success', handleData)
.on('error', handleError);
.then(handleData)
.catch(handleError);
```
## Have Fun !
Expand All @@ -209,7 +213,7 @@ function handleError (err) {
function newContact (email) {
mailjet.post('contact')
.request({Email: email})
.on('error', handleError);
.catch(handleError);
}
function testEmail (text) {
Expand All @@ -222,7 +226,7 @@ function testEmail (text) {
mailjet.post('send')
.request(email)
.on('error', handleError);
.catch(handleError);
}
testEmail('Hello World!');
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var mailjet = require ('./mailjet-client');
var mailjet = require('./mailjet-client')

module.exports = mailjet;
module.exports = mailjet
Loading

0 comments on commit b6a72f6

Please sign in to comment.