# raptor-render-context

> Helper module for working with promises

Latest version **1.0.2-beta** (published 2014-09-18) · Apache License v2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install raptor-render-context
pnpm add raptor-render-context
yarn add raptor-render-context
bun add raptor-render-context
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2-beta |
| Published | 2014-09-18 |
| First published | 2014-02-26 |
| Weekly downloads | 0 |
| License | Apache License v2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Patrick Steele-Idem |
| Maintainers | pnidem, philidem |

## Links

- npm: https://www.npmjs.com/package/raptor-render-context
- Repository: https://github.com/raptorjs3/raptor-render-context
- Issues: https://github.com/raptorjs3/raptor-render-context/issues
- npm.io page: https://npm.io/package/raptor-render-context

## Dependencies (3)

- [events](https://npm.io/package/events.md) ^1.0.2
- [raptor-logging](https://npm.io/package/raptor-logging.md) ^1.0.0-beta
- [raptor-strings](https://npm.io/package/raptor-strings.md) ^1.0.0-beta

## Recent versions

- 1.0.2-beta (latest) — 2014-09-18
- 1.0.1-beta — 2014-09-04
- 1.0.0-beta — 2014-09-04
- 0.2.24-beta — 2014-08-20
- 0.2.23-beta — 2014-08-12
- 0.2.22-beta — 2014-07-29
- 0.2.21-beta — 2014-07-29
- 0.2.20-beta — 2014-07-28
- 0.2.19-beta — 2014-07-23
- 0.2.18-beta — 2014-07-19
- 0.2.17-beta — 2014-06-19
- 0.2.16-beta — 2014-06-05
- 0.2.15-beta — 2014-05-27
- 0.2.14-beta — 2014-05-06
- 0.2.13-beta — 2014-05-05
- … 13 more at https://npm.io/package/raptor-render-context/versions

## README

raptor-render-context
=====================
The `raptor-render-context` module makes it possible to asynchrously render to an output stream while still flushing out bytes in the correct order. That is, the `raptor-render-context` module allow you to render parts of a stream out of order. The `raptor-render-context` module does the hard work of ensuring that the output bytes are flushed in the correct order. Content that is rendered before it is ready to be flushed is buffered and immediately flushed as soon it is ready.

An asynchronous render context is helpful if during rendering you have to wait for an asynchronous operation to complete before writing to part of a stream. As an example, you might start rendering a page to produce HTML and then get to a part of the page that depends on data that has not yet been loaded. With that use case, you can continue to render the remainder of the page and still pipe out the stream to the response. The asynchronous render context will ensure that the bytes are flushed out in the correct order.

# Installation

```
npm install raptor-render-context --save
```

# Usage

The simplest usage of an asynchronous render context is shown below:

```javascript
var through = require('through');

var output = '';
var stream = through(function write(data) {
        output += data;
    });

var context = require('raptor-render-context').create(stream)
    .on('error', function(err) {
        // Something went wrong during rendering
    })
    .on('end', function() {
        // Value of output: "ABC"
    });

context.write('A');
context.write('B');
context.write('C');
context.end();
```

Asynchronous, out-of-order rendering to an output stream is shown below:

```javascript
var through = require('through');

var output = '';
var stream = through(function write(data) {
        output += data;
    });

var context = require('raptor-render-context').create(stream)
    .on('error', function(err) {
        // Something went wrong during rendering
    })
    .on('end', function() {
        // Value of output: "ABC"
    });
    
context.write('A');

var asyncContext = context.beginAsync();
setTimeout(function() {
    asyncContext.write('B');
    asyncContext.end();
}, 1000);

context.write('C');
context.end();
```

You can also pipe another stream to a render context. For example, the following code illustrates how multiple templates could be rendered to the same asynchronous render context:

```javascript
var through = require('through');

var output = '';
var stream = through(function write(data) {
        output += data;
    });

var context = require('raptor-render-context').create(stream)
    .on('error', function(err) {
        // Something went wrong during rendering
    })
    .on('end', function() {
        // Value of output: "ABC"
    });

context.write('A');

var asyncContext = context.beginAsync();
require('fs').createReadStream('b.txt', 'utf8')
    .pipe(context);

context.write('C');
context.end();
```

---
_Source: https://npm.io/package/raptor-render-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
