Categories
JavaScript Answers

How to limit concurrency when using ES6’s Promise.all()?

Sometimes, we want to limit concurrency when using ES6’s Promise.all().

In this article, we’ll look at how to limit concurrency when using ES6’s Promise.all().

How to limit concurrency when using ES6’s Promise.all()?

To limit concurrency when using ES6’s Promise.all(), we can use the p-limit package.

To install it, we run

npm i p-limit

For instance, we write

const pLimit = require('p-limit');

const limit = pLimit(3);

const urls = [
  "http://www.exampleone.com/",
  "http://www.exampletwo.com/",
  "http://www.examplethree.com/",
  "http://www.examplefour.com/",
]

const promises = urls.map(url => {
  return limit(() => fetchData(url));
});

(async () => {
  const result = await Promise.all(promises);
  console.log(result);
})();

to call pLimit with 3 to return the limit function that limits the number of concurrent promises to 3.

Then we call urls.map with a callback that returns the promise that’s returned by limit.

And then call Promise.all with promises to return the results from the promises.

Conclusion

To limit concurrency when using ES6’s Promise.all(), we can use the p-limit package.

Categories
JavaScript Answers

How to fix the “Error: Cannot find module ‘express'” error with Node.js?

Sometimes, we want to fix the "Error: Cannot find module ‘express’" error with Node.js.

In this article, we’ll look at how to fix the "Error: Cannot find module ‘express’" error with Node.js.

How to fix the "Error: Cannot find module ‘express’" error with Node.js?

To fix the "Error: Cannot find module ‘express’" error with Node.js, we should install the express package.

To install it, we run

npm install express

Conclusion

To fix the "Error: Cannot find module ‘express’" error with Node.js, we should install the express package.

Categories
JavaScript Answers

How to configure the region in Node.js AWS SDK?

Sometimes, we want to configure the region in Node.js AWS SDK.

In this article, we’ll look at how to configure the region in Node.js AWS SDK.

How to configure the region in Node.js AWS SDK?

To configure the region in Node.js AWS SDK, we can call the aws.config.update method.

For instance, we write

const AWS = require('aws-sdk');
AWS.config.update({
  region: 'us-east-1'
});

const dd = new AWS.DynamoDB();
const s3 = new AWS.S3();

to set the region to 'us-east-1' with AWS.config.update.

Conclusion

To configure the region in Node.js AWS SDK, we can call the aws.config.update method.

Categories
JavaScript Answers

How to avoid deep nesting of asynchronous functions in Node.js?

Sometimes, we want to avoid deep nesting of asynchronous functions in Node.js.

In this article, we’ll look at how to avoid deep nesting of asynchronous functions in Node.js.

How to avoid deep nesting of asynchronous functions in Node.js?

To avoid deep nesting of asynchronous functions in Node.js, we can use the async.waterfall method.

We install the async package with

npm i async

Then we can use it by writing

const async = require("async");


async.waterfall([
  (callback) => {
    callback(null, 'one', 'two');
  },
  (arg1, arg2, callback) => {
    callback(null, 'three');
  },
  (arg1, callback) => {
    // arg1 now equals 'three'
    callback(null, 'done');
  }
], (err, result) => {
  // result now equals 'done'    
});

to call async.waterfall with an array of callback functions.

In each function, we call callback with the arguments we want so async.waterfall will proceed to calling the next callback in the array.

And when all the callbacks are called or if there’s an error, the callback in the 2nd argument is called.

Conclusion

To avoid deep nesting of asynchronous functions in Node.js, we can use the async.waterfall method.

Categories
JavaScript Answers

How to call Node.js fs.readFileSync with a relative path?

Sometimes, we want to call Node.js fs.readFileSync with a relative path.

In this article, we’ll look at how to call Node.js fs.readFileSync with a relative path.

How to call Node.js fs.readFileSync with a relative path?

To call Node.js fs.readFileSync with a relative path, we can call path.resolve with __dirname combined with the relative path.

For instance, we write

const path = require("path");
const file = fs.readFileSync(path.resolve(__dirname, "../file.xml"));

to call fs.readFileSync with the absolute path created by calling path.resolve with __dirname and '../file.xml'.

This will return the absolute path to file.xml which we can use to call readFileSync.

Conclusion

To call Node.js fs.readFileSync with a relative path, we can call path.resolve with __dirname combined with the relative path.