Created
May 2, 2010 05:27
-
-
Save coolaj86/386923 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <script> | |
| function make_promise() { | |
| var status = 'unresolved', | |
| outcome, | |
| waiting = [], | |
| dreading = []; | |
| function vouch(deed, func) { | |
| switch (status) { | |
| case 'unresolved': | |
| (deed === 'fulfilled' ? waiting : dreading).push(func); | |
| break; | |
| case deed: | |
| func(outcome); | |
| break; | |
| } | |
| }; | |
| function resolve(deed, value) { | |
| //$.jGrowl(JSON.stringify(status) + " " + JSON.stringify((status === 'unresolved')) ); | |
| if (status !== 'unresolved') { | |
| // TODO respond immediately instead | |
| //throw new Error('The promise has already been resolved:' + status); | |
| } | |
| status = deed; | |
| outcome = value; | |
| (deed == 'fulfilled' ? waiting : dreading).forEach(function (func) { | |
| try { | |
| func(outcome); | |
| } catch (ignore) {} | |
| }); | |
| waiting = null; | |
| dreading = null; | |
| }; | |
| return { | |
| when: function (func) { | |
| vouch('fulfilled', func); | |
| }, | |
| fail: function (func) { | |
| vouch('smashed', func); | |
| }, | |
| fulfill: function (value) { | |
| resolve('fulfilled', value); | |
| }, | |
| smash: function (string) { | |
| resolve('smashed', string); | |
| }, | |
| status: function () { | |
| return status; | |
| } | |
| }; | |
| }; | |
| // join any number of promises and return the results in the order they were passed in | |
| function join_promises () { | |
| var p = make_promise(), | |
| args = Array.prototype.slice.call(arguments,0), | |
| num = 0, | |
| ps = []; | |
| // Debug version only | |
| if (args[0].isArray) { | |
| if (args.length > 1) { | |
| var msg = "Multiple arrays of promises not supported"; | |
| DBG(msg); | |
| throw msg; | |
| } | |
| //args = args[0]; | |
| } | |
| // Production version | |
| args = (args[0].isArray ? args[0] : args); | |
| num = args.length; | |
| args.forEach(function (el, i, arr) { | |
| // increase the array to the appropriate size | |
| ps.push(undefined); | |
| el.when(function (data) { | |
| ps[i] = data; | |
| num -= 1; | |
| DBG(JSON.stringify(args) + ' ' + ps[i] + ' ' + num + ' ' + i ); | |
| if (0 === num) { | |
| DBG('Result: ' + JSON.stringify(ps)); | |
| p.fulfill.apply(p, ps); | |
| // p.fulfill.call(p, ps); | |
| } | |
| }); | |
| }); | |
| return p; | |
| } | |
| </script> | |
| <script> | |
| (function () { | |
| DBG = window.jQuery && window.jQuery.jGrowl || alert; | |
| var a = make_promise(), | |
| b = make_promise(), | |
| c = make_promise(), | |
| j; | |
| j = join_promises(a, b, c); | |
| j.when(function (d1, d2, d3) { | |
| DBG('End end result: ' + JSON.stringify(d1) + d2); | |
| //DBG('End end result: ' + d1 + d2 + d3); | |
| }); | |
| c.fulfill('World!'); | |
| a.fulfill('Hello'); | |
| b.fulfill(' '); | |
| }()); | |
| </script> | |
| <body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment