solve
v1.2.0
Published
Recursively converts asynchronous data into static data.
Downloads
89
Maintainers
Readme
solve
Recursively converts asynchronous data into static data.
Usage
var solve = require('solve');
var stream = solve(data, function (result) {
// called immediately, and whenever promises resolve or callbacks are called
});
// can add other listeners later
stream(function (result) {
// do something
});
// can chain additional streams
var downstream = stream(function (result) {
return result + 1;
});
downstream(function (newResult) {
// do something
});
// later, when we don't want downstream to call callbacks anymore
downstream.destroy();
Example
var solve = require('solve');
var data = {
foo: function(callback) {
setTimeout(function () {
callback('dynamic');
}, 1);
return 'static'
},
nested: function () {
return function () {
return function () {
return 'deep';
}
}
},
merge: solve({
promise: new Promise(function(resolve){
resolve('done');
})
})
};
solve(data, function(data) {
console.log(data);
});
This will output:
{ foo: 'static', nested: 'deep', merge: { promise: undefined } }
{ foo: 'static', nested: 'deep', merge: { promise: 'done' } }
{ foo: 'dynamic', nested: 'deep', merge: { promise: 'done' } }
Documentation
Read the tests for more details on what solve can do.
License
MIT