padolsey / operative

:dog2: Seamlessly create Web Workers
MIT License
656 stars 45 forks source link

Promises Support #7

Closed padolsey closed 11 years ago

padolsey commented 11 years ago

This enables Operative to return a promise for async operatives (per native Promise API).

The branch has been tested with the slightlyoff/Promises polyfill.

E.g.

var combine = operative(function(foo, bar) {
    // Internally, use a Deferred:
    var deferred = this.deferred();

    // A deferred has two methods: fulfill & reject:

    if (foo !== 'foo') {
        // Error (Rejection)
        deferred.reject('foo should be "foo"!');
    } else {
        // Success (Filfillment)
        deferred.fulfill(foo + bar);
    }
});

// Usage externally:
var promise = combine('foo', 'bar');

promise.then(function(value) {
    // Fulfilled
}, function(err) {
    // Rejected
});

As you can see this branch also includes support for operative(Function) in additional to the already supported operative(Object).

Note that operative will only return a Promise IF:

So, you'll have to include the aforementioned polyfill (or similar) to use this feature in most envs

Ref Issue #3, Issue #2

padolsey commented 11 years ago

TODO: Review implementation in light of https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure