-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from vizeat/feat/promise
Proper Promise Implementation
- Loading branch information
Showing
5 changed files
with
601 additions
and
583 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: {...}} | ||
}) | ||
|
||
``` | ||
|
||
|
@@ -109,8 +113,8 @@ user.request() | |
``` javascript | ||
|
||
sender.request({ Email: '[email protected]' }) | ||
.on('success', handleData) | ||
.on('error', handleError); | ||
.then(handleData) | ||
.catch(handleError); | ||
|
||
``` | ||
|
||
|
@@ -166,8 +170,8 @@ var emailData = { | |
sendEmail | ||
.request(emailData) | ||
.on('success', handlePostResponse) | ||
.on('error', handleError); | ||
.then(handlePostResponse) | ||
.catch(handleError); | ||
``` | ||
|
||
|
@@ -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 ! | ||
|
@@ -209,7 +213,7 @@ function handleError (err) { | |
function newContact (email) { | ||
mailjet.post('contact') | ||
.request({Email: email}) | ||
.on('error', handleError); | ||
.catch(handleError); | ||
} | ||
function testEmail (text) { | ||
|
@@ -222,7 +226,7 @@ function testEmail (text) { | |
mailjet.post('send') | ||
.request(email) | ||
.on('error', handleError); | ||
.catch(handleError); | ||
} | ||
testEmail('Hello World!'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.