malko / d.js Goto Github PK
View Code? Open in Web Editor NEWtiny promises/A+ implementation
Home Page: http://malko.github.io/D.js/
License: Other
tiny promises/A+ implementation
Home Page: http://malko.github.io/D.js/
License: Other
I couldn't find a way to tell D to not populate the browser's global namespace (window) with the D function. I was looking for something similar to the jQuery noConflict feature.
Hi,
when (https://github.com/cujojs/when) have a settle function that you can use to resolve or reject an array of promises.
It's similar to 'all' function, but does not call the 'error' callback if one of the promises is rejected, rather call the 'then' callback with an array of objects with 'state' (fulfilled/rejected) and the value/reason.
It is very useful, and would be nice addition to D.js.
Here is a trivial implementation (copy-pasted from 'all', I didn't have the time to make it nicer):
defer.settle = function(){ /*jshint loopfunc:true*/
var A=arguments;
if( A[length] === 1 && ( A[0] instanceof Array) ){
if( A[0][length] ){
return defer.settle.apply(defer,A[0]);
}else{
var D=defer();D[resolve](A[0]);
return D[promise];
}
}
var args=[]
, d=defer()
, promises=slice(A)
, c=promises[length]
;
if( ! c ){
d[resolve](args);
}else{
for( var i=0,l=c;i<l;i++){
(function(i){
var p = defer.promisify(promises[i]);
p[then](
function(v){
if( i in args ){ return; }
args[i] = { state: 'fulfilled', value: v };
(--c) || d[resolve](args);
}
,function(e){
if( i in args ){ return; }
args[i] = { state: 'rejected', reason: e };
(--c) || d[reject](e);
}
);
})(i);
}
}
return d[promise];
};
Googling for D.js is particularly difficult ๐
When processing pendings
, the return values of the callbacks are not persisted. This means that while the payload passed into the callback can be mutated, it cannot be entirely replaced (for example to wrap it in a decorator).
For example, the following will work as expected:
deferred = D();
deferred.promise.success(function(payload) {
payload.newProperty = true;
return payload;
});
deferred.resolve({});
deferred.promise.valueOf(); # { newProperty: true }
The following behaves unexpectedly (to me):
deferred = D();
deferred.promise.success(function(payload) {
payload = { newProperty: true };
return payload;
});
deferred.resolve({});
deferred.promise.valueOf(); # {}
This limitation prevents someone from wrapping the value, if it is an object, and also prevents other cases where someone may want to return an entirely different value.
I don't see anything about this behavior one way or another in the Promises/A+ spec, and this is already how native promises work. Is there any reason you wouldn't want to support this, perhaps configurable?
The AMD module definition states that
Module identifiers may not have file-name extensions like ".js".
Exporting "D.js" as the module id prevents AMD loaders from loading this dependency properly:
Line 434 in 5217f4c
Hello,
There is an undefined reference to a d variable in D.promisify (l:164), this breaks the D.promisify and D.all methods :
@@ -161,7 +161,7 @@
//-- if given value is not a promise return a fulfilled promise resolved to given value
defer.promisify = function(promise){
if(promise && isFunc(promise[then])){ return promise;}
- return d.resolved(promise);
+ return defer.resolved(promise);
};
And bonus question: I don't get the point of the closure (function(i){ ... })(i) in D.all, is there a reason?
Hi,
In order to pass this test suite:
https://github.com/promises-aplus/promises-tests
All you need to do is add the 'pending' API:
defer.pending = defer;
After that, the tests run smoothly.
Hi,
On npm node requirement is >=0.8, it would be nice if this requirement is announced in README.md or if it's not a real requirement to lower it to >=0.6 (since default node version is 0.6.x on debian and ubuntu)
Keep up the good work!
Thanks ;)
Hello
though not part of the promises/A+ spec I think it would be a nice addition to be able to specify the execution context of the callbacks.
For example with a bind method which store the context :
var defer = D().bind(this);
https://coveralls.io is an awesome service that calculates code's coverage. and it's free. Let's use it? :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.