Node.js Tutorials - 43 - Microtask Queues

Поделиться
HTML-код
  • Опубликовано: 6 окт 2024

Комментарии • 66

  • @muhammedsahad1612
    @muhammedsahad1612 Год назад +12

    The first time I saw the diagram, I skipped the entire tutorial, but after a few months, I started watching it again. The phase and queues in Node JS are now clear to me... thank you so much!

  • @Invalid_Username404
    @Invalid_Username404 Год назад +10

    Don't know what you have to apologize for. The switches between the diagram and the code are fantastic and help more. Thank you

    • @MathewAlex-hc4bf
      @MathewAlex-hc4bf Месяц назад

      the nexttick has priority over the promise right ? so in the last promise section the next tick should work immediately after the last callback right ?

  • @spellsaif
    @spellsaif Год назад +4

    Your teaching style is so so beautiful that I was able to predict the result. Thank you so much :)

  • @HarshVardhan-tk3ej
    @HarshVardhan-tk3ej 11 месяцев назад +1

    Core concepts explained with a heart winning simplicity.

  • @NyanTint
    @NyanTint Год назад +1

    You are one of the best teachers I ever met, sir. Everything explained well and short to the point. Got it in one go and I got my confidence boosted!

  • @shorty696820
    @shorty696820 Год назад +2

    Your explanation is the clearest one I've ever heard. Thanks a lot!

  • @chaitanyapatil337
    @chaitanyapatil337 Месяц назад

    Thanks for a crystal clear explanation. i have watched react series too and i learnt a lot as mostly focuses on practical part.

  • @gregthomas5887
    @gregthomas5887 Год назад +1

    I magically got it right on the first try thanks to your great explanations.

  • @amanpreetsingh2696
    @amanpreetsingh2696 21 час назад

    Too deep knowledge, that's also for free... thanks man

  • @privateanon7623
    @privateanon7623 11 месяцев назад +1

    The nextTick queue takes precedence over the microtask queue, but the example in (5:50 to 8:35) is only half correct. In fact, if someone runs it as an ESM script (.mjs file or with type="module" in package.json), the output will always be:
    2 -> 3 -> 1
    That's because instead of executing it synchronously, node schedules it as a microtask callback (like wrapping everything in queueMicroTask(() => { ... }), kind of). So after executing the code, the event loop grabs the next callback in the microtask queue until it becomes empty before checking the nextTick queue.
    Same thing for the 2nd example. Output for ESM would be:
    promise 1
    promise 2
    promise 3
    nextTick 1
    nextTick 2
    nextTick 3
    nextTick inside Promise
    nextTick inside nextTick

    • @Sarvesh_More
      @Sarvesh_More 8 месяцев назад

      hey i would really appreciate if you could provide me some reference where i can study about it. This behavior looks confusing.

    • @privateanon7623
      @privateanon7623 8 месяцев назад

      @@Sarvesh_More I understand the confusion.
      The closest thing to a reference that you can get is PR 45093 in the official github repo (/nodejs/node/pull/45093), specifically the review made by devsnek.
      It's an issue of erroneous documentation in the official docs and unfortunately, the PR addressing it has been stale for more than a year now. But basically, the mental model is the exact same, with the initial code being executed in the microtask queue

    • @Sarvesh_More
      @Sarvesh_More 8 месяцев назад

      @@privateanon7623 Thank you.

  • @AswiniduttBevara
    @AswiniduttBevara 9 месяцев назад

    Your teaching style is so so awesome. I'm able to predict the result. Thank you :)😀

  • @nithishnaidu5542
    @nithishnaidu5542 3 месяца назад +1

    11:57 I thought the nested nextTick ("nextTick inside nextTick") will be executed in the next event loop iteration.

  • @bytescoder
    @bytescoder 7 месяцев назад

    Simply outstanding🔥. U made it easy to catch those complex concepts 🎉

  • @mdsafikulislam7430
    @mdsafikulislam7430 Год назад +1

    Sir! You are great in this field!

  • @MinhLe-ow6op
    @MinhLe-ow6op 4 месяца назад

    Hi, your explanation is the clearest one I've ever heard. Thank you very much, but can I ask you for this slide for reference?

  • @sidhantkumarsingh2
    @sidhantkumarsingh2 26 дней назад

    beautifully explained

  • @nischalbista6436
    @nischalbista6436 Год назад

    your explanation is soooooo good that I got it right

  • @ajilvc5316
    @ajilvc5316 4 месяца назад

    Thank you so much for this tutorial. It helped me a lot

  • @pradneshkhedekar5279
    @pradneshkhedekar5279 Год назад

    Really great explanation and on point with visual representation 👍

  • @Shorts-zd4hn
    @Shorts-zd4hn Год назад

    It's been wonderful sir, and i got it right in the first try and you made it possible for me to guess

  • @abdullah-ayy
    @abdullah-ayy Год назад

    I always give your video a like before even watching it.

  • @YakshithKulal-t1o
    @YakshithKulal-t1o Год назад

    According to my understanding, the global function will also be removed from the call stack before running any async code

    • @igetom
      @igetom 11 месяцев назад +1

      no

  • @sahilmistry2898
    @sahilmistry2898 Год назад

    Greate explanation with patience👌👌

  • @AruljothySundramoorthy
    @AruljothySundramoorthy Месяц назад

    Awesome , nailed iT 🔥

  • @owethuweek6283
    @owethuweek6283 Год назад

    Thanks a lot. You have made my day!

  • @kerimagayev7330
    @kerimagayev7330 11 дней назад

    amazing

  • @chidimmaokafor7173
    @chidimmaokafor7173 Год назад +1

    I am a bit confused. I thought the inner CallBacks would join the next iteration. which would mean the sequence would be
    NextTick1,
    NextTick2,
    NextTick3,
    PromiseResolve1
    PromiseResolve2
    PromiseResolve3
    NextTick inside NextTick,
    Promise inside Promise

    • @privateanon7623
      @privateanon7623 11 месяцев назад

      That's nextTick/Microtask queues' quirk. The event loop will keep executing all the callbacks of each one until both are empty. You can block your program by abusing this mechanism.

  • @sanjarcode
    @sanjarcode Год назад

    Thanks a lot, Vishwas

  • @MathewAlex-hc4bf
    @MathewAlex-hc4bf Месяц назад

    But the nexttick has priority over the promise ,right . So nexttick should be executed in the promise section before the last promise callback ?

  • @LearnWithJK03
    @LearnWithJK03 Год назад

    Just wow concept 🙏🥰

  • @TyeThilah
    @TyeThilah 9 месяцев назад

    great job! thank you

  • @mohammedashiq2720
    @mohammedashiq2720 Год назад

    Well explanation

  • @rustolebayev3248
    @rustolebayev3248 Месяц назад

    promise 1
    promise 2
    nextTick inside promise
    promise 3
    i thought the order should be like that. because nextTick queue have priority higher than promise queue

  • @jivanmainali1742
    @jivanmainali1742 8 месяцев назад

    How does call stack knows to which queue it should put callbacks to ???

  • @-UCA-SAAMSHERONJ
    @-UCA-SAAMSHERONJ 2 месяца назад

    I thought after promise 2, nextTick inside Promise will execute as nextTick Queue is having hgher priority

  • @zallesov
    @zallesov 11 месяцев назад

    Also, I noticed that using Promises puts the execution of the handler to the next cycle. While using regular js callbacks execute right away in for example IO or Timer cycles. Is it fair to say that using callbacks is faster than using Promises? using Promise will add an additional cycle event if it is just Promise.resolve?

    • @hamza_dev
      @hamza_dev 11 месяцев назад

      Yes callbacks are a little bit faster but the difference is negligible specially when you consider the elegance and readability promises provide.

  • @webstartergurus4463
    @webstartergurus4463 5 месяцев назад

    Hello sir.. i got the promise.resolve.then consoled first before the process.nexttick console

  • @thirunavukarasusaminathan3102
    @thirunavukarasusaminathan3102 Год назад

    Yes i got the same results

  • @AlRashidashraf
    @AlRashidashraf Год назад

    Got right for next tick, but little different for promise.resolve,

  • @zallesov
    @zallesov 11 месяцев назад

    So Calling nextTick inside nextTick actually executes the callback in the same cycle and not in the next.

    • @privateanon7623
      @privateanon7623 11 месяцев назад

      It goes beyond that. NextTick and Microtask queues are emptied after every event loop action, rather than phase or iteration.

  • @faizanahmed9304
    @faizanahmed9304 Год назад

    Got right man!

  • @frontend_ko
    @frontend_ko Год назад

    thanks teacher 43

  • @rokckerzzzz6080
    @rokckerzzzz6080 Год назад

    Awesome Video...

  • @amankale4692
    @amankale4692 Год назад

    *space bar* 4:06 ~ "That's what she said". *space bar*

  • @gobicorner
    @gobicorner 10 месяцев назад

    i got that right

  • @pronaypramanick4467
    @pronaypramanick4467 Год назад

    Hey, I have a question. Why did you repeatedly use the phrase 'user-written synchronous code' in your explanations? Why do we have to mention 'user written'?

  • @kalpeshjadvani7358
    @kalpeshjadvani7358 6 месяцев назад

    Promise.resolve().then(() => console.log('start resolved promise'));
    process.nextTick(() => console.log('start nextTick'));
    I have run this in my local but bit confusing for me. I am getting
    [nodemon] restarting due to changes...
    [nodemon] starting `node justDemo.js`
    start resolved promise
    start nextTick
    [nodemon] clean exit - waiting for changes before restart
    As you can see I got in same order, not able to understand.

  • @natarajanyuvaraj5265
    @natarajanyuvaraj5265 Год назад

    Got it right

  • @iqbalali9796
    @iqbalali9796 Год назад

    In my case promise queue running before process.nextTick queue

  • @andrewshaban2888
    @andrewshaban2888 Год назад

    Why did they make it so complex? Why do we need so many queues?

    • @privateanon7623
      @privateanon7623 11 месяцев назад +1

      The answer is rather complicated and is related to how operating systems handle each type of task, but in essence, each queue is made for some kind of work that has its own quirks.
      For example, timers are organized in an ordered heap that puts expired timers in the timers queue, while close functions are low priority and only need to be pushed as they come (FIFO).

  • @rishiraj2548
    @rishiraj2548 Год назад

    🙏👍👍

  • @ashokyadavfqplmgxjia4668
    @ashokyadavfqplmgxjia4668 6 месяцев назад

    sir where is your feet i want touch it for it's clear explaination.............thanks sir.........always respect for great teacher.......

  • @Pareshbpatel
    @Pareshbpatel 8 месяцев назад

    {2024-01-30}

  • @igetom
    @igetom 11 месяцев назад +2

    Good tutorial but, god, your ending of all senteces is so annoying

  • @mohitpal1505
    @mohitpal1505 5 месяцев назад

    Promise.resolve().then(() => console.log("this is Promise 1"));
    process.nextTick(() => console.log("nextTick 1"));
    Why output is actually coming :
    this is Promise 1
    nextTick 1