1.0.5 • Published 4 years ago

static-observable v1.0.5

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

static-observable Build Status js-standard-style

Static observable methods next, error, and complete. Equivalent to Promise.resolve() and Promise.reject() for promises.

Installation

npm i --save static-observable
npm i --save rxjs # peer dependency

Why?

For quick return cases and test stubs

Usage

Example: chain

var StaticObservable = require('static-observable')

var observable = StaticObservable
  .next({ foo: 1 })
  .next({ foo: 2 })
  .complete()

observable.subscribe(
  function (next) { console.log('NEXT:', next )},
  function (err) { console.log('ERROR:', err )},
  function () { console.log('COMPLETE' )}
)
// NEXT: { foo: 1 }
// NEXT: { foo: 2 }
// COMPLETE

Example: error chain

var StaticObservable = require('static-observable')

var observable = StaticObservable
  .next({ foo: 1 })
  .next({ foo: 2 })
  .error(new Error('boom'))

observable.subscribe(
  function (next) { console.log('NEXT:', next )},
  function (err) { console.log('ERROR:', err )},
  function () { console.log('COMPLETE' )}
)
// NEXT: { foo: 1 }
// NEXT: { foo: 2 }
// ERROR: [Error: boom]

Example: error and complete are "safe"

var StaticObservable = require('static-observable')

var observable = StaticObservable
  .error(new Error('boom'))
  .next({ foo: 1 })

observable.subscribe(
  function (next) { console.log('NEXT:', next )},
  function (err) { console.log('ERROR:', err )},
  function () { console.log('COMPLETE' )}
)
// ERROR: [Error: boom]

var observable = StaticObservable
  .complete(new Error('boom'))
  .next({ foo: 1 })

observable.subscribe(
  function (next) { console.log('NEXT:', next )},
  function (err) { console.log('ERROR:', err )},
  function () { console.log('COMPLETE' )}
)
// COMPLETE

License

MIT