Last active
August 29, 2015 13:57
-
-
Save wonglok/9406897 to your computer and use it in GitHub Desktop.
Promise Loop
This file contains 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
"use strict"; | |
console.log('============'); | |
(function(){ | |
var i = 0; | |
var iLast = window.performance.now(); | |
//console.profile("Processing pixels"); | |
function loop(){ | |
if (i === 300){ | |
//console.profileEnd(); | |
return; }; | |
var waitTime = window.performance.now() - iLast; | |
if (waitTime > 1000/30){ | |
i = 0; | |
//console.timeStamp('bad bad jank!!') | |
console.error('bad bad jank!!! wait time between frame: ',waitTime); | |
}else{ | |
console.log('no jank detected'); | |
} | |
iLast = window.performance.now(); | |
i++; | |
requestAnimationFrame(loop); | |
} | |
requestAnimationFrame(loop); | |
}()) | |
function fib(n) { | |
return n < 2 ? 1 : fib(n - 1) + fib(n - 2); | |
} | |
//----------------------- | |
function promiseForEach(list, handler, existingChain){ | |
return list | |
.reduce(function (pChain, item, index, array){ | |
return pChain.then(function (){ | |
return handler(item, index, array); | |
}); | |
}, existingChain || Promise.resolve() ); | |
} | |
function promiseForEachCopy(){ | |
arguments[0] = arguments[0].slice(0); | |
return promiseLooper.apply(this,arguments); | |
} | |
function resolvePromiseList(promiseList, eachThen, existingChain){ | |
return promiseList.reduce(function(pChain, eachPromise) { | |
return pChain.then(function() { | |
return eachPromise; | |
}).then(eachThen); | |
}, existingChain || Promise.resolve()) | |
} | |
//---------------------- | |
var processData = function (item,index,array){ | |
console.log('from:',item,'to:', item * 2); | |
array[index] = item * 2; | |
return array; | |
}; | |
var genPromiseToList = function (item,index,array){ | |
var waitTime = 1000 * (index+1) * Math.random(); | |
array[index] = new Promise(function(resolve,reject){ | |
var ans = 'result: '+ fib(30) + '~' + item + '@waited '; | |
setTimeout(function(){ | |
resolve(ans) ; | |
//simluate different time resolve; | |
}, waitTime); | |
}); | |
console.log('task:',index, 'with wait time: ', waitTime); | |
return array; | |
}; | |
var lastTime = window.performance.now(); | |
var eachThennable = function(data){ | |
console.log(data, window.performance.now() - lastTime); | |
lastTime = window.performance.now(); | |
}; | |
//---------------- | |
var a = [1,2,3,4,5,6,7,8,9,10]; | |
var b = Promise.resolve(a) | |
//transform data | |
.then(function(data){ | |
console.log('level1 transfrom data',data); | |
return promiseForEach(data,processData); | |
}) | |
//start parallel task | |
.then(function(data){ | |
console.log('level2 generate parallel task',data); | |
return promiseForEach(data,genPromiseToList); | |
}) | |
//complete parallel task with sequence, progreeisve enhancement | |
.then(function(promises){ | |
console.log('level3 resolve parallel task in sequence',promises); | |
return resolvePromiseList(promises, eachThennable); | |
}); | |
console.log('------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment