Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added promised.js

  • Loading branch information...
commit b5023c32e740c8244827cb63b11acfdad8c86a3f 1 parent 4d7356b
Victor Hugo Borja authored
44 examples/jquery-parallel.js
... ... @@ -0,0 +1,44 @@
  1 +var mecha = require('mecha')
  2 + , expect = require('chai').expect
  3 +
  4 + // libraries
  5 + , Q = require( 'q' )
  6 + , async = require( 'async' )
  7 + , P = require( '../promised' ).Q(Q)
  8 +
  9 + // example objects
  10 + , a = Q.defer()
  11 + , b = Q.defer()
  12 + , c = Q.defer()
  13 +
  14 +async.parallel( P.get(a, b), P.set(c) )
  15 +
  16 +c.promise.then( function( result ) {
  17 + mecha.log( "Result is", result )
  18 +}, function( error ) {
  19 + mecha.log( "ERROR is", error )
  20 +})
  21 +
  22 +setTimeout( function() { a.resolve( "Hello" ) }, 400 )
  23 +setTimeout( function() { b.resolve( "World" ) }, 200 )
  24 +
  25 +describe("Convert a Q deferred into a callback", function(){
  26 +
  27 + it("should resolve c to an array", function(done){
  28 + c.promise.then(function( result ) {
  29 + expect(result).to.include("Hello", "World")
  30 + done()
  31 + })
  32 + })
  33 +
  34 + it("should leave the array in correct order", function(done){
  35 + c.promise.then(function( result ) {
  36 + expect(result[0]).to.equal("Hello")
  37 + expect(result[1]).to.equal("World")
  38 + done()
  39 + })
  40 + })
  41 +
  42 +})
  43 +
  44 +mecha({reporter: 'nyan'})
29 package.json
... ... @@ -0,0 +1,29 @@
  1 +{
  2 + "author": "Victor Hugo Borja <vic.borja@gmail.com> (http://twitter.com/vborja)",
  3 + "name": "promised",
  4 + "description": "Convert back and forth from javascript Promises to Callbacks",
  5 + "version": "0.0.0",
  6 + "homepage": "http://vic.github.com/promised",
  7 + "repository": {
  8 + "type": "git",
  9 + "url": "git://github.com/vic/promised.git"
  10 + },
  11 + "main": "promised.js",
  12 + "scripts": {
  13 + "test": "mocha"
  14 + },
  15 + "dependencies": {},
  16 + "devDependencies": {
  17 + "mocha": "*",
  18 + "chai": "*",
  19 + "mecha": "*"
  20 + },
  21 + "optionalDependencies": {
  22 + "jquery": "*",
  23 + "q": "*",
  24 + "async": "*"
  25 + },
  26 + "engines": {
  27 + "node": "*"
  28 + }
  29 +}
146 promised.js
... ... @@ -0,0 +1,146 @@
  1 +new (function(module){
  2 +
  3 + var identity = function(i) { return i }
  4 +
  5 + var Promised = function(deferrer, promiser) {
  6 +
  7 + if(!deferrer){
  8 + deferrer = identity
  9 + }
  10 + if(!promiser){
  11 + promiser = identity
  12 + }
  13 +
  14 + var fwd = function(p) {
  15 + return function(arg, callback){
  16 + var promise = promiser(arg)
  17 + if(p) {
  18 + promise.then(function(value){ callback(value) })
  19 + } else {
  20 + promise.then(function(value){ callback(null, value) },
  21 + function(value){ callback(value) })
  22 + }
  23 + }
  24 + }
  25 +
  26 + var bwd = function(p) {
  27 + return function(arg){
  28 + return function(error, value) {
  29 + var deferred = deferrer(arg)
  30 + if (p && arguments.length < 2) {
  31 + deferred.resolve(arguments[0])
  32 + } else if(p) {
  33 + deferred.resolve(Array.prototype.slice.apply(arguments, []))
  34 + } else if (error) {
  35 + deferred.reject(error)
  36 + } else if (arguments.length > 2) {
  37 + deferred.resolve(Array.prototype.slice.apply(arguments, [2]))
  38 + } else {
  39 + deferred.resolve(value)
  40 + }
  41 + }
  42 + }
  43 + }
  44 +
  45 + var ret = function(p) {
  46 + return function(f, reciever){
  47 + var args = Array.prototype.slice.apply(arguments, [2])
  48 + , curry = function(){
  49 + var args = Array.prototype.slice.apply(arguments, [])
  50 + , func = function(){
  51 + var argv = Array.prototype.slice.apply(arguments, [])
  52 + , callback = argv.pop()
  53 + , value = f.apply(reciever, args.concat(argv))
  54 + , deferred = deferrer(value)
  55 + fwd(p)(deferred, callback)
  56 + }
  57 + func.curry = curry
  58 + return func
  59 + }
  60 + return curry.apply(null, args)
  61 + }
  62 + }
  63 +
  64 + var get = function(p) {
  65 + return function() {
  66 + var promises = Array.prototype.slice.apply(arguments, [])
  67 + , callbacks = []
  68 + , getter = function(arg) {
  69 + callbacks.push(function(callback) {
  70 + fwd(p)(arg, callback)
  71 + })
  72 + }
  73 + promises.forEach(getter)
  74 + return callbacks.length == 1 ? callbacks[0] : callbacks
  75 + }
  76 + }
  77 +
  78 + var set = function(p) {
  79 + return function() {
  80 + var deferreds = Array.prototype.slice.apply(arguments, [])
  81 + , callbacks = []
  82 + , setter = function(arg) {
  83 + callbacks.push(bwd(p)(arg))
  84 + }
  85 + deferreds.forEach(setter)
  86 + return function(){
  87 + if(p) {
  88 + for(var i in callbacks){
  89 + callbacks[i].apply(this, arguments[i])
  90 + }
  91 + } else {
  92 + for(var i in callbacks){
  93 + callbacks[i].apply(this, arguments)
  94 + }
  95 + }
  96 + }
  97 + }
  98 + }
  99 +
  100 + var promised = ret(false)
  101 + promised.fwd = fwd(false)
  102 + promised.bwd = bwd(false)
  103 + promised.get = get(false)
  104 + promised.set = set(false)
  105 +
  106 + promised.p = ret(true)
  107 + promised.p.fwd = fwd(true)
  108 + promised.p.bwd = bwd(true)
  109 + promised.p.get = get(true)
  110 + promised.p.set = set(true)
  111 +
  112 + return promised
  113 + }
  114 +
  115 + Promised.$ = function($) {
  116 + return Promised(function(value){
  117 + if(value && typeof value.promise === 'function') {
  118 + return value
  119 + } else {
  120 + return $.Deferred().resolve(value)
  121 + }
  122 + }, function(deferred){
  123 + return deferred.promise()
  124 + })
  125 + }
  126 +
  127 + Promised.Q = function(Q) {
  128 + return Promised(function(value){
  129 + if(value && typeof value.promise === 'object') {
  130 + return value
  131 + } else {
  132 + var deferred = Q.defer()
  133 + deferred.resolve(value)
  134 + return deferred
  135 + }
  136 + }, function(deferred){
  137 + return deferred.promise
  138 + })
  139 + }
  140 +
  141 + module.exports = Promised})( typeof module !== 'undefined' ? module : new function(global, name){
  142 + Object.defineProperty(this, 'exports', {
  143 + set: function(value){ global[name] = value },
  144 + get: function() { return global[name] }
  145 + })
  146 +}(this, 'promised'))
1  test/examples.js
... ... @@ -0,0 +1 @@
  1 +require('../examples/q-async')

0 comments on commit b5023c3

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