panzerdp / dmitripavlutin.com-comments

7 stars 0 forks source link

javascript-callback/ #99

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Everything About Callback Functions in JavaScript

The callback is a function being called by another function, either synchronously or asynchronously.

https://dmitripavlutin.com/javascript-callback/

vijeysrc commented 3 years ago

setTimeout(callback, 0) execute the callback asynchronously

panzerdp commented 3 years ago

setTimeout(callback, 0) execute the callback asynchronously

Exactly!

srk1195 commented 3 years ago

setTimeout(cb,0) will be asynchronous, since the function fundamentally cannot behave differently upon different delays supplied.

hopecookies commented 3 years ago

love all the content! here's a small typo under section 4, the first sentence:

"The special keyword async placed before the function definition creates an asynchornous function:" misspelled asynchronous.

still, love all the content. keep them coming!

baspinarenes commented 3 years ago

Simple and clear explanation. Thanks!

panzerdp commented 3 years ago

Simple and clear explanation. Thanks!

Thanks @baspinarenes!

SilverWings47 commented 3 years ago

Can you explain to me how does the arrow function ({ login }) => login work in " users.map(({ login }) => login);" Is login an object ?? Thank you.

ghkerh commented 2 years ago

Hello Dmitri! Excellent explanation of the "callback" question! But in the array.map function, is it possible to integrate an additional callback to intervene between each item of the array? If not, how should I proceed then? (Because map will not wait for my setTimeout and other braking attempts!)

panzerdp commented 2 years ago

Hello Dmitri! Excellent explanation of the "callback" question! But in the array.map function, is it possible to integrate an additional callback to intervene between each item of the array? If not, how should I proceed then? (Because map will not wait for my setTimeout and other braking attempts!)

Hi @ghkerh! Can you share some code?

ghkerh commented 2 years ago

Hi Dmitri! Thank you for your answer!!! I have on the screen 5 rows of 11 thumbnails each. By 'callback' I treat each row separately to get special effects (by changing the CSS class of each thumbnail). But all the thumbnails of the same row change at the same time. Impossible to process them one by one...

Code (original function called: runThumbs): function runThumbs() { if ((document.getElementById('size').value === '1') && (vG.anim)) { let classMP, u = 0; vG.nroRow = 0; moveThumbs(animeThumbs); } }

function isEven(n) { n = Number(n); return n === 0 || !!(n && !(n % 2)); }

function moveThumbs(fct) { let classMP; if (isEven(vG.nroRow)) classMP = 'thumbEffectM'; else classMP = 'thumbEffectP'; vG.classMP = classMP; if (vG.nroRow < 5) { fct(); vG.nroRow++; setTimeout(function() {moveThumbs(fct);}, 150); // This works perfectly for the rows! } else return; }

function animeThumbs() { function fctCB(idTh) { document.getElementById(idTh).classList.add(classMP); } function fcuCB(idTh) { document.getElementById(idTh).classList.remove(classMP); } let classMP = vG.classMP; let varR = 'rowmult' + vG.nroRow.toString(); const row = document.getElementById(varR); if (row) { // 1. let thumbs = []; for (let i = 0; i < row.children.length; i++) {thumbs.push(document.getElementById(row.id).children[i].id);} thumbs.map(fctCB); // Here I would like clearly distinguish the movement of each thumbnail on a row. // 2. setTimeout(function() {thumbs.map(fcuCB);}, 1000); // ...and here } }

Le mer. 5 janv. 2022 à 18:09, Dmitri Pavlutin @.***> a écrit :

Hello Dmitri! Excellent explanation of the "callback" question! But in the array.map function, is it possible to integrate an additional callback to intervene between each item of the array? If not, how should I proceed then? (Because map will not wait for my setTimeout and other braking attempts!)

Hi @ghkerh https://github.com/ghkerh! Can you share some code?

— Reply to this email directly, view it on GitHub https://github.com/panzerdp/dmitripavlutin.com-comments/issues/99#issuecomment-1005911686, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIKS4OGLJ636FHKRZD2S4ADUUR3NRANCNFSM4VM2OLCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

RostoRM commented 2 years ago

"The synchronous callbacks are executed at the same time as the higher-order function that uses the callback."

setTimeout( () => console.log('This is callback'), 0);

I thought this was synchronous callbacks...as it's execute at the same time as the higher-order function... Can anyone explain why is this asynchronously?

I think this was asynchronously callback like this:

setTimeout( () => console.log('This is callback'), 3000);

AdeetyAraJsaH commented 1 year ago

users.map(({ login }) => login) I couldn't get this , can you explain inside code of map function?