Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Extending info support for promises and for lists of lifecycle callbacks #765

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions concepts/ORM/Lifecyclecallbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Lifecycle callbacks are functions that are automagically called before or after

Sails exposes a handful of lifecycle callbacks by default.

Lifecycle callbacks may be provided as array of functions to be invoked sequentially, too.

##### Callbacks on `create`

Expand Down
14 changes: 13 additions & 1 deletion reference/waterline/queries/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@ The primary purpose of Waterline query instances is to provide a convenient, cha

### Promises

In addition to the `.exec()` method, Waterline queries implement a partial integration with the [Bluebird](https://github.com/petkaantonov/bluebird) promise library, exposing `.then()` and `.catch()` methods.
In addition to the `.exec()` method, Waterline queries implement a partial integration with the [Bluebird](https://github.com/petkaantonov/bluebird) promise library, exposing `.toPromise()`,
`.then()` and `.catch()` methods.

```js
Stuff.find()
.then(function (allTheStuff) {...})
.catch(function (err) {...});
```

`.toPromise()` is actually executing query. It is invoked implicitly by `.then()` or `.catch()`. Using `.toPromise()` itself
is convenient if you want to utilize promise-based helper methods e.g. for running queries in parallel.

```js
require("bluebird").all([
Stuff.find().toPromise(),
OtherStuff.find().toPromise()
])
.spread(function (allTheStuff, allTheOtherStuff) {...})
.catch(function (err) {...});
```

If you are a fan of promises, and have a reasonable amount of experience with them, you should have no problem working with this interface. However if you are not very familiar with promises, or don't care one way or another, you will probably have an easier time working with `.exec()`, which uses standard Node.js callback conventions.

Expand Down