https://github.com/nichoth/pull-from-api
Create streams for high latency async functions
https://github.com/nichoth/pull-from-api
Last synced: over 1 year ago
JSON representation
Create streams for high latency async functions
- Host: GitHub
- URL: https://github.com/nichoth/pull-from-api
- Owner: nichoth
- Created: 2017-01-25T00:11:40.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-25T00:31:54.000Z (over 9 years ago)
- Last Synced: 2025-01-13T17:26:26.882Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://github.com/nichoth/pull-from-api#readme
- Size: 2.93 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# pull from api
Return a stream that emits three events: `start`, `resolve`, and either `data` or `error`.
## example
```js
var test = require('tape')
var S = require('pull-stream')
var fromFn = require('../')
var fromObject = require('../from-object')
function mock (arg, cb) {
process.nextTick(cb.bind(null, null, arg + ' response'))
}
var testErr = new Error('test')
function mockErr (cb) {
process.nextTick(cb.bind(null, testErr))
}
test('create stream from function', function (t) {
t.plan(2)
var stream = fromFn(mock)('test')
S(
stream,
S.collect(function (err, res) {
t.error(err)
t.deepEqual(res, [
['start', { args: ['test'], op: null}],
['data', 'test response'],
['resolve', { args: ['test'], op: null}],
], 'should return the right events')
})
)
})
test('create streams from object', function (t) {
t.plan(2)
var fns = {
foo: mock,
bar: mock
}
var streamFns = fromObject(fns)
S(
streamFns.foo('test'),
S.collect(function (err, res) {
t.error(err)
t.deepEqual(res, [
['start', { args: ['test'], op: 'foo'}],
['foo', 'test response'],
['resolve', { args: ['test'], op: 'foo'}],
], 'should namespace with the keys')
})
)
})
test('error event', function (t) {
t.plan(2)
var stream = fromFn(mockErr)()
S(
stream,
S.collect(function (err, res) {
t.error(err)
t.deepEqual(res, [
['start', { args: [], op: null }],
['error', testErr],
['resolve', { args: [], op: null }],
], 'should return the right events')
})
)
})
```