promise-flow
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -11,2 +11,3 @@ 'use strict'; | ||
exports.mapSeries = mapSeries; | ||
exports.entangledCallback = entangledCallback; | ||
@@ -70,2 +71,39 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
} | ||
} | ||
function entangledCallback(optional_value_resolver) { | ||
var resolve_fn = undefined; | ||
var reject_fn = undefined; | ||
var promise = new Promise(function (resolve, reject) { | ||
resolve_fn = resolve; | ||
reject_fn = reject; | ||
}); | ||
var callback = function callback(err) { | ||
for (var _len = arguments.length, values = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
values[_key - 1] = arguments[_key]; | ||
} | ||
if (err) { | ||
reject_fn(err); | ||
return; | ||
} | ||
if (!optional_value_resolver) { | ||
resolve_fn(values[0]); | ||
return; | ||
} | ||
var value = undefined; | ||
try { | ||
value = optional_value_resolver.apply(undefined, values); | ||
} catch (value_resolver_err) { | ||
reject_fn(value_resolver_err); | ||
return; | ||
} | ||
resolve_fn(value); | ||
}; | ||
return [promise, callback]; | ||
} |
{ | ||
"name": "promise-flow", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Utility module for functions that return promises", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -26,3 +26,3 @@ [![Build Status](https://travis-ci.org/dbrockman/promise-flow.svg?branch=master)](https://travis-ci.org/dbrockman/promise-flow) | ||
Signature `allObject<T>(object: { [key: string]: Promise<T> | T }): Promise<{ [key: string]: T }>` | ||
> `allObject<T>(object: { [key: string]: Promise<T> | T }): Promise<{ [key: string]: T }>` | ||
@@ -43,3 +43,3 @@ Returns a promise that resolves with a copy of the input object when all values have resolved. | ||
Signature `series<T>(factories: Array<() => Promise<T> | T): Promise<Array<T>>` | ||
> `series<T>(factories: Array<() => Promise<T> | T): Promise<Array<T>>` | ||
@@ -62,3 +62,3 @@ Takes an array of functions that will be executed in series. Each one will wait until the previous function is done. | ||
Signature `parallel<T>(factories: Array<() => Promise<T> | T): Promise<Array<T>>` | ||
> `parallel<T>(factories: Array<() => Promise<T> | T): Promise<Array<T>>` | ||
@@ -70,3 +70,3 @@ Same as `series` but will run all functions in parallel. | ||
Signature `map<T, U>(array: Array<T>, closure: (value: T, index: number) => Promise<U> | U): Promise<Array<U>>` | ||
> `map<T, U>(array: Array<T>, closure: (value: T, index: number) => Promise<U> | U): Promise<Array<U>>` | ||
@@ -78,4 +78,42 @@ Map the items in the array with a function that may return a promise. | ||
Signature `map<T, U>(array: Array<T>, closure: (value: T, index: number) => Promise<U> | U): Promise<Array<U>>` | ||
> `map<T, U>(array: Array<T>, closure: (value: T, index: number) => Promise<U> | U): Promise<Array<U>>` | ||
Same as `map` but will run all functions in series. | ||
### `entangledCallback(optional_value_resolver)` | ||
> `entangledCallback<T>(optional_value_resolver?: (...values: Array<any>) => T): [Promise<T>, (err: ?Error, ...values: Array<any>) => void]` | ||
Returns a tuple where the first item is a promise and the second item is a node-style callback function. The two are connected so that when the callback is invoked, the promise will be resolved. | ||
An optional function can be provided to reduce arguments passed to the callback to a single value. | ||
```js | ||
// Example where a function that takes a callback is used: | ||
import { readFile } from 'fs'; | ||
// Example function that calls readFile and pass the entangled callback instead of wrapping the call in new Promise(...) | ||
function myReadFile(filename, options) { | ||
const [promise, callback] = promise_flow.entangledCallback(); | ||
readFile(filename, options, callback); | ||
return promise; | ||
} | ||
// myReadFile will return a promise that is either resolved with the file content or rejected with the error from readFile. | ||
myReadFile('./file.txt').then(handleFileData, handleReadError); | ||
``` | ||
```js | ||
// Example callback invocation and the equivalent state of the entangled promise: | ||
const [promise, callback] = promise_flow.entangledCallback(); | ||
// callback() is equivalent to Promise.resolve() | ||
// callback(null, 'value') is equivalent to Promise.resolve('value') | ||
// callback(null, 'a', 'b', 'c') is equivalent to Promise.resolve('a') | ||
// callback(new Error('error')) is equivalent to Promise.reject(new Error('error')) | ||
// The optional function argument is used to reduce values passed to the callback to a single value | ||
const [promise, callback] = promise_flow.entangledCallback(Array.of); | ||
// callback(null, 'a') is equivalent to Promise.resolve(['a']) | ||
// callback(null, 'a', 'b', 'c') is equivalent to Promise.resolve(['a', 'b', 'c']) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9438
110
115