1
\$\begingroup\$

example:

exports.products = (req, res) => {
  // var1
  const ITEM_LIMIT = 4;
  // var2
  const page = +req.query.p || 1;


  // unimportant part
  productModel.getProdCount().then(([count]) => {
    const totalItems = count[0]['COUNT(id)'];
    productModel.fetchAllProd((page - 1) * ITEM_LIMIT, ITEM_LIMIT).then(([rows]) => {
      res.render('general/products', {
        prods: rows,
        title: 'Products Page',
        path: '/products',
        currPage: page,
        hasNextPage: ITEM_LIMIT * page < totalItems,
        havePrevPage: page > 1,
        prevPage: page - 1,
        nextPage: page + 1,
        lastPage: Math.ceil(totalItems / ITEM_LIMIT),
      });
    });
  });
};

As you see I have var1 and var2.

Is it ok to define them as sync like this?

Should I create an async structure for them? What is best practice.

If I use it heavily in this way, for example, if 10 million users run the same function, I think the event loop may get blocked.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Your code is a perfectly fine non-blocking async code, now lets talk about your questions one by one.

Is it ok to define them as sync like this?

Yes its perfectly fine to have variables declared in a non async function as you have it above.

Should I create an async structure for them?

You may choose to for improving code readability or if thats your style however it will make no difference other than changing return type of your function products from void to Promise.

If I use it heavily in this way, for example, if 10 million users run the same function, I think the event loop may get blocked.

No, event loop will not get blocked, please note Javascript is single threaded so at any point only one resolved promise code will be available in execution stack rest resolved Promises will be waiting in queue for their turn, however if you have a large userbase as the number you mentioned you may wanna look into Node clusters which basically creates multiple instance of your application thus giving you option of utilizing full potential of your CPU and handling multiple requests simultaneously.

Also Note:

The variables in your code will be created each time function products() is called and will be a closure to inner promises however they wont be shared across multiple executions, i.e if products() is executed more than once (typically for another request) both executions will have values independent of each other.

However if you choose to declare a variable outside of function something like :

// var1
const ITEM_LIMIT = 4;
exports.products = (req, res) => {
  //...

Each execution of this function will share same value of ITEM_LIMIT, which again will not be an issue depending on number if issues, however can cause logical issues say you update the value but need original value in next execution.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.