Very simple but complete Promise implementation.
- Implements the Promises/A+ 1.1.0 specification:
Promise.prototype.then(onResolve, onReject)
.
- Implements the ES6 Promise draft:
new Promise(function(resolve, reject){})
,Promise.all(iterable)
,Promise.race(iterable)
,Promise.reject(reason)
,Promise.resolve(value)
,Promise.prototype.catch(onReject)
.
- Implements some of the earlier proposed features for Promises/A:
Promise.prototype.get(propertyName)
,Promise.prototype.call(methodName)
,- Third optional argument
onProgress
tothen
:Promise.prototype.then(onResolve, onReject, onProgress)
.
- And to actually be able to use the progress handler:
- Third argument
progress
for the executor function passed tonew Promise
:new Promise(function(resolve, reject, progress){})
.
- Third argument
- Tests against the Promises/A+ tests as well as some "local" tests (most of which are adapted copies if tests found in domenic/promises-unwrapping) for the functionality that's not part of Promises/A+.
function get(url){
var promise = new Promise(function(resolve, reject, progress){
var request = new XMLHttpRequest();
request.addEventListener('load', function(){
if (request.status == 200){
resolve(request.response);
} else {
reject(new Error(request.statusText));
}
}, false);
request.addEventListener('progress', progress, false);
request.addEventListener('error', reject, false);
request.addEventListener('abort', reject, false);
request.open('GET', url, true);
request.send();
});
return promise;
}
get('/some/path').then(
function(response){
console.log(response);
},
function(reason){
console.log(reason);
},
function(event){
console.log(event);
}
);