mini-deferred
v0.3.3
Published
A mini deferred plugin, having just what you need.
Downloads
2
Readme
deferred
A mini deferred plugin, having just what you need.
This module is published with UMD support, globally accessible via $def variable. It is also available on npm named mini-deferred.
NPM Installation
npm install mini-deferred --save
var $def = require('mini-deferred');
Usage $def method
var promise = $def(function(resolve,reject,notify){
$.ajax({
url : "/api",
success : function(data){
//do something
resolve(data);
},
error : reject,
progress : notify
})
});
promise.then(function(data){
//do something with success data
return data + "Some new data";
},function(err){
//handle error
},function(value){
//display progress
})
//chain promises
.then(function(data){
//do something else with success data
return data + "Some new data";
},function(err){
//handle error
},function(value){
//display progress
})
Creating a deferred object $def.defer()
var defer = $def.defer();
$.ajax({
url : "/api2",
success : function(data){
//do something
defer.resolve(data);
},
error : defer.reject,
progress : defer.notify
})
var promise2 = defer.promise();
Doing something after multiple promise $def.when()
$def.when(promise,promise2).then(function(data){
//data will be arry of resolve data
//[promise 1 resolve data, promise 2 resolve data]
},function(err){
//handle error
})
promise.then()
promise.then(onSuccess,onError,onProgress);
promise.done()
promise.done(onSuccess);
promise.fail()
promise.fail(onError);
promise.progress()
promise.progress(onProgress);
promise.always()
promise.always(function(data){
// comes here whether it's success or fail
});
promise.state() // return state of promise