Created
August 19, 2016 08:11
-
-
Save Grassboy/f4dd7c117466fd315547b3c5a23231d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//{{ PromisePool | |
/* Usage: | |
var promise_pool = new PromisePool([concurrency_limit]); | |
* concurrency_limit: n of concurrency promices executions | |
generate a PromisePool | |
promise_pool.push(fn); | |
* function: the function to generate promise object | |
push a function into PromisePool, this functipn will generate promise object | |
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, | |
push: function(fn){ | |
this.queue.push(fn); | |
}, | |
start: function(){ | |
var that = this; | |
var p = Promise.resolve(); | |
var results = []; | |
var process = function(group){ | |
return Promise.all(group.map(function(fn){ | |
return fn(); | |
})).then(function(r){ | |
results.push.apply(results, r); | |
console.log(results.length, 'results merge done'); | |
return results; | |
}); | |
}; | |
while(that.queue.length) { | |
p = p.then( | |
process.bind(that, that.queue.splice(0, that.limit)) | |
); | |
} | |
return p; | |
} | |
}; | |
////}} | |
//{{ 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); | |
promise_list.push(fnToGeneratePromise2); | |
promise_list.push(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.bind(this, 'user1')); | |
promise_list.push(getUserData.bind(this, '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