Skip to content

Instantly share code, notes, and snippets.

@kirbysayshi
Forked from kriskowal/LICENSE
Created August 6, 2011 05:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirbysayshi/1129049 to your computer and use it in GitHub Desktop.
Save kirbysayshi/1129049 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

This is a modification, allowing for multiple arguments to be passed to the "resolve" method.

function (
a, // placeholder for pending callbacks
b // placeholder for fulfilled value
) {
a = []; // callbacks or 0 if fulfilled
return {
resolve: function () { // fulfillment arguments
b = arguments; // store the fulfilled value(s)
// send the value to every pre-registered callback
while (a.length)
a.shift().apply({}, 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.apply({}, b) // otherwise, let it know what the fulfilled value
// was immediately
}
}
}
function(a,b){a=[];return{resolve:function(){b=arguments;while(a.length)a.shift().apply({},b);a=0},then:function(c){a?a.push(c):c.apply({},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(){b=arguments;while(a.length)a.shift().apply({},b);a=0},then:function(c){a?a.push(c):c.apply({},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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment