de

Simple asynchronous promise-based controller

npm install de
22 downloads in the last week
39 downloads in the last month

de Build Status

Simple asynchronous promise based data agent

Lazy example


var Exception;
var Promise;
var StdProvider;
var StdRuntime;

var agent;

Exception = require('de/util/Exception');
Promise = require('jspromise');
StdProvider = require('de/Provider');
StdRuntime = require('de/util/Runtime');

function Runtime () {
  StdRuntime.apply(this, arguments);
}

Runtime.prototype = Object.create(StdRuntime.prototype);

//  my Runtime turbo extension
Runtime.prototype.getRequest = function () {
  return this.params.request;
};

function Provider () {
  StdProvider.apply(this, arguments);
}

Provider.prototype = Object.create(StdProvider.prototype);

//  @overrides default method
Provider.prototype._createRuntime = function (params) {

  return new Runtime(this, params);
};

agent = new Provider();

//  data declaration (polymorphic)
agent.decl('my-data-3', {
  deps: ['my-data-1', 'my-async-data', 'url', 'some-bad'],
  data: function (result, errors) {

    console.log(errors['some-bad']); // 9000

    return result['my-data-1'] + result.myAsyncData + url.query.some;
  },
  alias: 'myData3'
});

agent.decl('my-data-1', 42);

agent.decl('my-async-data', {
  alias: 'myAsyncData',
  deps: 'url',
  data: function (result, errors) {

  //  the only way to return data asynchronously (U can use ANY compilant promises/A+ implementation)
    var promise;

    promise = new JSPromise();

    setTimeout(function () {
      promise.fulfill(result.url.query.data));
    }, 5000);

    return promise;
  }
});

agent.decl('url', function () {

  var req;

  req = this.getRequest();

  console.log(this instanceof Runtime); //  true

  return Url.parse(req.url, true);
});

agent.decl('some-bad', function () {
  throw 9000;
});

//  data providing
agent.provide({request: {
  url: '/a/b/c/?data=5&some=6'
} }, ['my-data-3']).then(function (data) {

  console.log(data.result.myData3); // 5 + 6 + 42
});

To reject provide() promise u can throw an {de/util/Exception} instance inside data function. data can be any value or promise object, not only function

npm loves you