Categories
JavaScript Answers

How to time out a Promise if failed to complete in time with Node.js and JavaScript?

Sometimes, we want to time out a Promise if failed to complete in time with Node.js and JavaScript.

In this article, we’ll look at how to time out a Promise if failed to complete in time with Node.js and JavaScript.

How to time out a Promise if failed to complete in time with Node.js and JavaScript?

To time out a Promise if failed to complete in time with Node.js and JavaScript, we use the Promise.race method.

For instance, we write

const withTimeout = (millis, promise) => {
  const timeout = new Promise((resolve, reject) =>
    setTimeout(() => reject(`Timed out after ${millis} ms.`), millis)
  );
  return Promise.race([promise, timeout]);
};

to define the withTimeout function.

In it, we create the timeout promise, which calls setTimeout callback when millis milliseconds has elapsed.

We call reject to reject the promise.

Then we call Promise.race with an array with the promise we want to run and the timeout promise.

Then the result of the promise that finishes first is returned as a promise.

Conclusion

To time out a Promise if failed to complete in time with Node.js and JavaScript, we use the Promise.race method.

Categories
JavaScript Answers

How to remove characters from a string with JavaScript?

Sometimes, we want to remove characters from a string with JavaScript.

In this article, we’ll look at how to remove characters from a string with JavaScript.

How to remove characters from a string with JavaScript?

To remove characters from a string with JavaScript, we use the string replace method.

For instance, we write

const str = "foo bar baz";
const newStr = str.replace(/ba/gi, "");

to call str.replace with a regex that matches the pattern we want to remove.

And we replace all matches with empty strings.

The g flag makes replace replace all matches.

i matches it find matches in a case-insensitive manner.

Conclusion

To remove characters from a string with JavaScript, we use the string replace method.

Categories
JavaScript Answers

How to move item in array to last position with JavaScript?

Sometimes, we want to move item in array to last position with JavaScript.

In this article, we’ll look at how to move item in array to last position with JavaScript.

How to move item in array to last position with JavaScript?

To move item in array to last position with JavaScript, we use the shift and push methods.

For instance, we write

const a = [5, 1, 2, 3, 4];
a.push(a.shift());

to call a.shift to return the first item from array a after removing it.

Then we call a.push with the return item to put it as the last item in a.

Conclusion

To move item in array to last position with JavaScript, we use the shift and push methods.

Categories
JavaScript Answers

How to get value of span text with JavaScript?

Sometimes, we want to get value of span text with JavaScript.

In this article, we’ll look at how to get value of span text with JavaScript.

How to get value of span text with JavaScript?

To get value of span text with JavaScript, we use the innerText property.

For instance, we write

<span id="span_Id">I am the Text </span>

to add a span.

Then we write

const spanText = document.getElementById("span_Id").innerText;

console.log(spanText);

to select the span with getElementById.

Then we get the text content with the innerText property.

Conclusion

To get value of span text with JavaScript, we use the innerText property.

Categories
JavaScript Answers

How to add optional first argument in function with JavaScript?

Sometimes, we want to add optional first argument in function with JavaScript.

In this article, we’ll look at how to add optional first argument in function with JavaScript.

How to add optional first argument in function with JavaScript?

To add optional first argument in function with JavaScript, we can make our function accept an object.

For instance, we write

const myFunction = (hash) => {
  const { options, content } = hash;
  //...
};

myFunction({ options, content });

to define the myFunction function that takes the hash object.

In it, we get the options and content properties from the hash.

And we call myFunction with an object with those 2 properties.

We can omit any property in the object we call myFunction with.

Conclusion

To add optional first argument in function with JavaScript, we can make our function accept an object.