node package manager

solve

Recursively converts asynchronous data into static data.

solve

Recursively converts asynchronous data into static data.

NPM version Downloads Bower version

Build Status Coverage Status Dependency status Dev Dependency status

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