1

In order to test some of the concepts regarding sync vs async functions in node.js I would like to create an example emulating a blocking function, how can I achieve this? More specific, I want to demonstrate that using Express I can implement two GET requests, where the first one blocks the second one when executed in parallel. How should blockingFunctionHere in the following example looks like:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/1', function(req, res, next) {

  blockingFunctionHere(); // emulate a blocking function for some seconds, let's say 10 seconds

  res.render('index', { title: 'Coming from request #1' });
});

router.get('/2', function(req, res, next) {

    res.render('index', { title: 'Coming from request #2' })
});

module.exports = router;

4
  • 2
    @DaveNewton — Timeouts are non-blocking. Commented Dec 17, 2019 at 13:47
  • @Quentin Oh duh; I misunderstood the question. Commented Dec 17, 2019 at 13:49
  • set a flag whose value will change after the timeout and before executing the second function check if the flag changed! Commented Dec 17, 2019 at 13:53
  • @landrykapela that's not blocking either Commented Dec 17, 2019 at 15:44

2 Answers 2

3

I'd do it using a while loop that checks the time difference between when the function was called vs the current iteration time.

As long as the time difference doesn't exceed a passed delay, keep looping. Since it's a synchronous loop, the Event Loop would be blocked for the duration of the looping.

const block = delay => {
  const now = (new Date()).getTime()
  
  while (((new Date()).getTime() - now) <= delay) {
    // do nothing
  }
}

block(3000) // 3 seconds
console.log('end')

Don't expect this to be time accurate. This emulates, usually unwanted, "blocking" behaviour for around 3 seconds.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, is what I was looking for. I have an extra question, I have read that async functions are non-blocking by defintion, but what does happen if I include such a loop inside an async function? If I am not work it blocks anyway, then why so many people says that async functions are non-blocking? I don't get it...
It's gonna block, putting a long running blocking piece of code in an async function doesn't magically make it run on a separate thread.
2

You can create a blocking function by using a loop. Check out much time has passed and let the loop condition pass after 10 seconds.

function blocking() {
    const start = moment()
    let waiting = true;
    while (waiting) {
        const now = moment();
        const time = now.diff(start, "seconds");
        if (time > 10) {
            waiting = false;
        }
    }
}

alert("Before");
blocking();
alert("After");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.