The Strapi SQL dialect provides several options to deal with query output. The following methods are present on the query builder, schema builder, and the raw builder.
## Promises
[Promises](https://github.com/petkaantonov/bluebird#what-are-promises-and-why-should-i-use-them) are the preferred way of dealing with queries in the dialect, as they allow you to return values from a fulfillment handler, which in turn become the value of the promise. The main benefit of promises are the ability to catch thrown errors without crashing the node app, making your code behave like a `.try` / `.catch` / `.finally` in synchronous code.
Coerces the current query builder chain into a promise state, accepting the resolve and reject handlers as specified by the [Promises/A+ spec](http://promises-aplus.github.com/promises-spec). As stated in the spec, more than one call to the `then` method for the current query chain will resolve with the same value, in the order they were called; the query will not be executed multiple times.
Executes side effects on the resolved response, ultimately returning a promise that fulfills with the original value. A thrown error or rejected promise will cause the promise to transition into a rejected state.
Using only `.then()`:
```js
query.then(function (x) {
doSideEffectsHere(x);
return x;
});
```
Using `.tap()`:
```js
promise.tap(doSideEffectsHere);
```
### .map(mapper)
A passthrough to [Bluebird's map implementation](https://github.com/petkaantonov/bluebird/blob/master/API.md#mapfunction-mapper---promise) with the result set.
A passthrough to [Bluebird's reduce implementation](https://github.com/petkaantonov/bluebird/blob/master/API.md#reducefunction-reducer--dynamic-initialvalue---promise) with the result set.
A passthrough to [Bluebird's bind method](https://github.com/petkaantonov/bluebird/blob/master/API.md#binddynamic-thisarg---promise) which sets the context value (this) for the returned promise.
If you'd prefer a callback interface over promises, the `asCallback` function accepts a standard node style callback for executing the query chain. Note that as with the `then` method, subsequent calls to the same query chain will return the same result.
Streams are a powerful way of piping data through as it comes in, rather than all at once. You can read more about streams [here at substack's stream handbook](https://github.com/substack/stream-handbook). See the following for example uses of stream & pipe. If you wish to use streams with PostgreSQL, you must also install the [pg-query-stream module](https://github.com/brianc/node-pg-query-stream). On an HTTP server, make sure to manually close your streams if a request is aborted.
### .stream([options], [callback])
If called with a callback, the callback is passed the stream and a promise is returned. Otherwise, the readable stream is returned.
A `query` event is fired just before a query takes place, providing data about the query, including the connection's `__cid` property and any other information about the query as described in `toSQL`. Useful for logging all queries throughout your application.
A `query-error` event is fired when an error occurs when running a query, providing the error object and data about the query, including the connection's `__cid` property and any other information about the query as described in `toSQL`. Useful for logging all query errors throughout your application.