Skip to content

Instantly share code, notes, and snippets.

@Grassboy
Created August 16, 2016 11:10
Show Gist options
  • Save Grassboy/7dcb18e0f6848618bac612be3a160f78 to your computer and use it in GitHub Desktop.
Save Grassboy/7dcb18e0f6848618bac612be3a160f78 to your computer and use it in GitHub Desktop.
//{{ PromisePool
/* Usage:
var promise_pool = new PromisePool([concurrency_limit]);
* concurrency_limit: n of concurrency promices executions
generate a PromisePool
promise_pool.push(function, args);
* function: the function to generate promise object
* args: the arguments array of function
push a promise object into PromisePool, but the real Promise object has *NOT* been generated yet
promise_pool.start();
generate and execute all Promises in pool base on the concurrency_limit
return a Promise object resolved when all Promises object in pool are resolved/rejected
*/
var PromisePool = function(limit) {
this.queue = [];
this.limit = (limit > 0) ? limit: 10;
};
PromisePool.prototype = {
constructor: PromisePool,
_dequeue: function() {
var that = this;
var group = that.queue.splice(0, that.limit);
console.log(group.length, 'promises for process');
group.forEach(function(p, i){
group[i] = p[0].apply(this, p[1]);
});
return Promise.all(group);
},
push: function(fn, args){
this.queue.push([fn, args]);
},
start: function(){
var that = this;
return new Promise(function(resolve, reject){
var results = [];
(function process(){
that._dequeue().then(function(r){
console.log(r.length, 'done');
results.push.apply(results, r);
console.log(results.length, 'results merge done');
if(that.queue.length != 0) {
console.log(that.queue.length, 'remains');
process();
} else {
console.log(results.length, 'all done');
resolve(results);
}
});
})();
});
}
};
////}}
//{{ Scenario 1
// Old Scheme
var promise_list = [];
promise_list.push(new Promise(function(resolve, reject){
// promise 1
}));
promise_list.push(new Promise(function(resolve, reject){
// promise 2
}));
promise_list.push(new Promise(function(resolve, reject){
// promise 3
}));
Promise.all(promise_list).then(function(results){
console.log('all done');
});
// New Scheme
var promise_list = new PromisePool(10);
promise_list.push(fnToGeneratePromise1, [/*args of fnToGeneratePromise1*/]);
promise_list.push(fnToGeneratePromise2, [/*args of fnToGeneratePromise2*/]);
promise_list.push(fnToGeneratePromise3, [/*args of fnToGeneratePromise3*/]);
promise_list.start().then(function(results){
console.log('all done');
});
////}}
//{{ Scenario 2
/* jQuery included... */
var getUserData = function(user_id){
return new Promise(resolve, reject) {
$.get('getUserData.php?user_id='+user_id).then(function(rsp){
resolve(rsp);
});
};
};
// Old Scheme
var promise_list = [];
promise_list.push(getUserData('user1'));
promise_list.push(getUserData('user2'));
// :
// Skip lots of promise_list.push
// :
promise_list.push(getUserData('user100'));
Promise.all(promise_list).then(function(results){
console.log('all user data loaded');
});
// New Scheme
var promise_list = new PromisePool(10);
promise_list.push(getUserData, ['user1']);
promise_list.push(getUserData, ['user2']);
// :
// Skip lots of promise_list.push
// :
promise_list.push(getUserData, ['user100']);
promise_list.start().then(function(results){
console.log('all user data loaded');
});
//}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment