From 52e2211625b5cfca41d0266cd1e09b7671c5b012 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Sun, 7 Jun 2015 13:42:45 +0800 Subject: [PATCH] Update bluebird --- bluebird.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/bluebird.md b/bluebird.md index 5870ef022..4dca377de 100644 --- a/bluebird.md +++ b/bluebird.md @@ -96,3 +96,25 @@ See [Promisification](https://github.com/petkaantonov/bluebird/blob/master/API.m var readFile = Promise.promisify(fs.readFile); var fs = Promise.promisifyAll(require('fs')); ``` + +### Promise-returning methods +See [Promise.method](https://github.com/petkaantonov/bluebird/blob/master/API.md#promisemethodfunction-fn---function) to allow `return`ing values that will be promise resolutions. + +```js +User.login = Promise.method(function(email, password) { + if (!valid) + throw new Error("Email not valid"); + + return /* promise */; +}); +``` + +### Generators +See [Promise.coroutine](https://github.com/petkaantonov/bluebird/blob/master/API.md#promisecoroutinegeneratorfunction-generatorfunction---function). + +``` +User.login = Promise.coroutine(function* (email, password) { + let user = yield User.find({email: email}).fetch(); + return user; +}); +```