Skip to content
Browse files

Added promised.js

  • Loading branch information...
1 parent 4d7356b commit b5023c32e740c8244827cb63b11acfdad8c86a3f @vic committed Aug 25, 2012
Showing with 220 additions and 0 deletions.
  1. +44 −0 examples/jquery-parallel.js
  2. +29 −0 package.json
  3. +146 −0 promised.js
  4. +1 −0 test/examples.js
View
44 examples/jquery-parallel.js
@@ -0,0 +1,44 @@
+var mecha = require('mecha')
+ , expect = require('chai').expect
+
+ // libraries
+ , Q = require( 'q' )
+ , async = require( 'async' )
+ , P = require( '../promised' ).Q(Q)
+
+ // example objects
+ , a = Q.defer()
+ , b = Q.defer()
+ , c = Q.defer()
+
+async.parallel( P.get(a, b), P.set(c) )
+
+c.promise.then( function( result ) {
+ mecha.log( "Result is", result )
+}, function( error ) {
+ mecha.log( "ERROR is", error )
+})
+
+setTimeout( function() { a.resolve( "Hello" ) }, 400 )
+setTimeout( function() { b.resolve( "World" ) }, 200 )
+
+describe("Convert a Q deferred into a callback", function(){
+
+ it("should resolve c to an array", function(done){
+ c.promise.then(function( result ) {
+ expect(result).to.include("Hello", "World")
+ done()
+ })
+ })
+
+ it("should leave the array in correct order", function(done){
+ c.promise.then(function( result ) {
+ expect(result[0]).to.equal("Hello")
+ expect(result[1]).to.equal("World")
+ done()
+ })
+ })
+
+})
+
+mecha({reporter: 'nyan'})
View
29 package.json
@@ -0,0 +1,29 @@
+{
+ "author": "Victor Hugo Borja <vic.borja@gmail.com> (http://twitter.com/vborja)",
+ "name": "promised",
+ "description": "Convert back and forth from javascript Promises to Callbacks",
+ "version": "0.0.0",
+ "homepage": "http://vic.github.com/promised",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/vic/promised.git"
+ },
+ "main": "promised.js",
+ "scripts": {
+ "test": "mocha"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "chai": "*",
+ "mecha": "*"
+ },
+ "optionalDependencies": {
+ "jquery": "*",
+ "q": "*",
+ "async": "*"
+ },
+ "engines": {
+ "node": "*"
+ }
+}
View
146 promised.js
@@ -0,0 +1,146 @@
+new (function(module){
+
+ var identity = function(i) { return i }
+
+ var Promised = function(deferrer, promiser) {
+
+ if(!deferrer){
+ deferrer = identity
+ }
+ if(!promiser){
+ promiser = identity
+ }
+
+ var fwd = function(p) {
+ return function(arg, callback){
+ var promise = promiser(arg)
+ if(p) {
+ promise.then(function(value){ callback(value) })
+ } else {
+ promise.then(function(value){ callback(null, value) },
+ function(value){ callback(value) })
+ }
+ }
+ }
+
+ var bwd = function(p) {
+ return function(arg){
+ return function(error, value) {
+ var deferred = deferrer(arg)
+ if (p && arguments.length < 2) {
+ deferred.resolve(arguments[0])
+ } else if(p) {
+ deferred.resolve(Array.prototype.slice.apply(arguments, []))
+ } else if (error) {
+ deferred.reject(error)
+ } else if (arguments.length > 2) {
+ deferred.resolve(Array.prototype.slice.apply(arguments, [2]))
+ } else {
+ deferred.resolve(value)
+ }
+ }
+ }
+ }
+
+ var ret = function(p) {
+ return function(f, reciever){
+ var args = Array.prototype.slice.apply(arguments, [2])
+ , curry = function(){
+ var args = Array.prototype.slice.apply(arguments, [])
+ , func = function(){
+ var argv = Array.prototype.slice.apply(arguments, [])
+ , callback = argv.pop()
+ , value = f.apply(reciever, args.concat(argv))
+ , deferred = deferrer(value)
+ fwd(p)(deferred, callback)
+ }
+ func.curry = curry
+ return func
+ }
+ return curry.apply(null, args)
+ }
+ }
+
+ var get = function(p) {
+ return function() {
+ var promises = Array.prototype.slice.apply(arguments, [])
+ , callbacks = []
+ , getter = function(arg) {
+ callbacks.push(function(callback) {
+ fwd(p)(arg, callback)
+ })
+ }
+ promises.forEach(getter)
+ return callbacks.length == 1 ? callbacks[0] : callbacks
+ }
+ }
+
+ var set = function(p) {
+ return function() {
+ var deferreds = Array.prototype.slice.apply(arguments, [])
+ , callbacks = []
+ , setter = function(arg) {
+ callbacks.push(bwd(p)(arg))
+ }
+ deferreds.forEach(setter)
+ return function(){
+ if(p) {
+ for(var i in callbacks){
+ callbacks[i].apply(this, arguments[i])
+ }
+ } else {
+ for(var i in callbacks){
+ callbacks[i].apply(this, arguments)
+ }
+ }
+ }
+ }
+ }
+
+ var promised = ret(false)
+ promised.fwd = fwd(false)
+ promised.bwd = bwd(false)
+ promised.get = get(false)
+ promised.set = set(false)
+
+ promised.p = ret(true)
+ promised.p.fwd = fwd(true)
+ promised.p.bwd = bwd(true)
+ promised.p.get = get(true)
+ promised.p.set = set(true)
+
+ return promised
+ }
+
+ Promised.$ = function($) {
+ return Promised(function(value){
+ if(value && typeof value.promise === 'function') {
+ return value
+ } else {
+ return $.Deferred().resolve(value)
+ }
+ }, function(deferred){
+ return deferred.promise()
+ })
+ }
+
+ Promised.Q = function(Q) {
+ return Promised(function(value){
+ if(value && typeof value.promise === 'object') {
+ return value
+ } else {
+ var deferred = Q.defer()
+ deferred.resolve(value)
+ return deferred
+ }
+ }, function(deferred){
+ return deferred.promise
+ })
+ }
+
+ module.exports = Promised})( typeof module !== 'undefined' ? module : new function(global, name){
+ Object.defineProperty(this, 'exports', {
+ set: function(value){ global[name] = value },
+ get: function() { return global[name] }
+ })
+}(this, 'promised'))
View
1 test/examples.js
@@ -0,0 +1 @@
+require('../examples/q-async')

0 comments on commit b5023c3

Please sign in to comment.
Something went wrong with that request. Please try again.