Categories
JavaScript Answers

How to use filesystem in Node.js with async and await?

Sometimes, we want to use filesystem in Node.js with async and await.

In this article, we’ll look at how to use filesystem in Node.js with async and await.

How to use filesystem in Node.js with async and await?

To use filesystem in Node.js with async and await. we can use the fs.promises module.

For instance, we write

import fs from "fs";
const fsPromises = fs.promises;

const listDir = async () => {
  try {
    return fsPromises.readdir("path/to/dir");
  } catch (err) {
    console.error("Error", err);
  }
};

listDir();

to create the listDir function that calls fsPromises.readdir to get the contents listing of the directory.

We return the promise returned by readdir to return the results in the promise.

Conclusion

To use filesystem in Node.js with async and await. we can use the fs.promises module.

Categories
JavaScript Answers

How to fix scrollIntoView scrolling too far with JavaScript?

To fix scrollIntoView scrolling too far with JavaScript, we can call scrolTo with the top position of the element.

For instance, we write

const id = "profilePhoto";
const yOffset = -10;
const element = document.getElementById(id);
const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset;

window.scrollTo({ top: y, behavior: "smooth" });

to get the element we want to scroll to with getElementById.

Then we get the y position by adding the top positionb of the element with the pageYOffset and yOffset.

Finally, we call scrollTo with an object with the top position set to y to scroll to the top of the element.

Categories
JavaScript Answers

How to get notified about changes of the history via history.pushState with JavaScript?

Sometimes, we want to get notified about changes of the history via history.pushState with JavaScript.

In this article, we’ll look at how to get notified about changes of the history via history.pushState with JavaScript.

How to get notified about changes of the history via history.pushState with JavaScript?

To get notified about changes of the history via history.pushState with JavaScript, we can listen for changes in webNavigation.onHistoryStateUpdated.

To do this, we write

browser.webNavigation.onHistoryStateUpdated.addListener(listener, filter);

to call browser.webNavigation.onHistoryStateUpdated.addListener with the listener to listen for history.pushState calls with the listenr function.

Conclusion

To get notified about changes of the history via history.pushState with JavaScript, we can listen for changes in webNavigation.onHistoryStateUpdated.

Categories
JavaScript Answers

How to escape a JSON string containing newline characters using JavaScript?

Sometimes, we want to escape a JSON string containing newline characters using JavaScript.

In this article, we’ll look at how to escape a JSON string containing newline characters using JavaScript.

How to escape a JSON string containing newline characters using JavaScript?

To escape a JSON string containing newline characters using JavaScript, we can call string replace to replace various characters.

For instance, we write

const escape = (str) => {
  return str
    .replace(/[\\]/g, "\\\\")
    .replace(/[\"]/g, '\\"')
    .replace(/[\/]/g, "\\/")
    .replace(/[\b]/g, "\\b")
    .replace(/[\f]/g, "\\f")
    .replace(/[\n]/g, "\\n")
    .replace(/[\r]/g, "\\r")
    .replace(/[\t]/g, "\\t");
};

to replace the unescaped characters in the first argument with the escaped characters in the 2nd.

Conclusion

To escape a JSON string containing newline characters using JavaScript, we can call string replace to replace various characters.

Categories
JavaScript Answers

How to check if element is in array with JavaScript?

Sometimes, we want to check if element is in array with JavaScript.

In this article, we’ll look at how to check if element is in array with JavaScript.

How to check if element is in array with JavaScript?

To check if element is in array with JavaScript, we can use the array includes method.

For instance, we write

if (arr.includes("abc")) {
  // ...
}

to call arr.includes with 'abc' to check if the arr array has 'abc' in it.

Conclusion

To check if element is in array with JavaScript, we can use the array includes method.