Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Forked from 140bytes/LICENSE.txt
Created May 17, 2011 17:52
Show Gist options
  • Save kriskowal/976971 to your computer and use it in GitHub Desktop.
Save kriskowal/976971 to your computer and use it in GitHub Desktop.
Nano-promises

Provides a single "defer()" function that returns an object that is both a deferred and a thenable promise. It supports multiple listeners. It does not support promise composition (since "then" returns nothing), forwarding (since "resolve" does not accept further deferred promises), and it does not support error handling (since "then" doesn't accept an errback). It unfortunately does support multiple resolution.

For other variations and design rationales, read: https://github.com/kriskowal/q/tree/master/design

(function (
a, // placeholder for pending callbacks
b // placeholder for fulfilled value
) {
a = []; // callbacks or 0 if fulfilled
return {
resolve: function (c) { // fulfillment value
b = c; // store the fulfilled value
// send the value to every pre-registered callback
while (a.length)
a.shift()(b);
// switch state to "fulfilled"
a=0
},
then: function (c) { // callback
a ? // if it's not fulfilled yet
a.push(c) : // added the callback to the queue
c(b) // otherwise, let it know what the fulfilled value
// was immediately
}
}
})
(function(a,b){a=[];return{resolve:function(c){b=c;while(a.length)a.shift()(b);a=0},then:function(c){a?a.push(c):c(b)}}})
Copyright 2011 Kristopher Michael Kowal. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
{
"name": "nano-q",
"keywords": ["q", "defer", "thenable", "promise", "deferred"]
}
var defer = (function(a,b){a=[];return{resolve:function(c){b=c;while(a.length)a.shift()(b);a=0},then:function(c){a?a.push(c):c(b)}}});
var x = defer();
setTimeout(function () {
x.resolve("Hello, World!");
}, 500);
setTimeout(function () {
x.resolve("Die in a fire!");
}, 1000);
x.then(function (message) {
console.log(message);
});
x.then(function (message) {
console.log(message);
});
@kirbysayshi
Copy link

I modified this slightly (still under 140) to allow for multiple arguments to "resolve", here: https://gist.github.com/1129049

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment