Skip to content

Instantly share code, notes, and snippets.

@dmitrykuznetsovdev
Created December 5, 2016 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitrykuznetsovdev/aaf7510e8db70cc88b9238a7b561f7be to your computer and use it in GitHub Desktop.
Save dmitrykuznetsovdev/aaf7510e8db70cc88b9238a7b561f7be to your computer and use it in GitHub Desktop.
callstack block
/**
* поставит в очередь Callback Queue стразу
*/
setTimeout(() => {
console.log('setTimeout', 1);
}, 0)
setTimeout(() => {
console.log('setTimeout', 2);
}, 0)
setTimeout(() => {
console.log('setTimeout', 3);
}, 0)
setTimeout(() => {
console.log('setTimeout', 4);
}, 0)
/**
* поставит в очередь Callback Queue только через 500 mc
*/
setTimeout(() => {
console.log('setTimeout', 5);
}, 500)
setTimeout(() => {
console.log('setTimeout', 6);
}, 500)
setTimeout(() => {
console.log('setTimeout', 7);
}, 500)
setTimeout(() => {
console.log('setTimeout', 8);
}, 500)
setTimeout(() => {
console.log('setTimeout', 9);
}, 500)
setTimeout(() => {
console.log('setTimeout', 10);
}, 500)
function asyncLoop(arr, cb){
arr.forEach((index)=>{
setTimeout(cb.bind(null, index), 0);
})
}
/**
* заблокирует весь call stack
* поставит в очередь обработчики
*
* после их выполнения, запустит те что поставились через 500 mc
*/
asyncLoop([1, 2, 3, 4], (index)=>{
for (let i = 0; i < 3000000000; i++) {
}
console.log(index, "index");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment