Categories
JavaScript Answers

How to dynamically chain JavaScript promises?

Sometimes, we want to dynamically chain JavaScript promises.

In this article, we’ll look at how to dynamically chain JavaScript promises.

How to dynamically chain JavaScript promises?

To dynamically chain JavaScript promises, we can use the for-of loop and async and await.

For instance, we write:

const myAsyncFuncs = [
  (val) => {
    return Promise.resolve(val + 1);
  },
  (val) => {
    return Promise.resolve(val + 2);
  },
  (val) => {
    return Promise.resolve(val + 3);
  },
];

(async () => {
  for (const f of myAsyncFuncs) {
    const v = await f(1)
    console.log(v)
  }
})()

We have the myAsyncFuncs array that has functions that returns promises.

Then we can run each function sequentially by creating an async function that loops through the functions with a for-of loop.

We get the resolved value of each promise with await.

Therefore, we see 2, 3, and 4 logged for v.

Conclusion

To dynamically chain JavaScript promises, we can use the for-of loop and async and await.

Categories
JavaScript Answers

How to open a PDF in a new browser full window with JavaScript?

Sometimes, we want to open a PDF in a new browser full window with JavaScript.

In this article, we’ll look at how to open a PDF in a new browser full window with JavaScript.

How to open a PDF in a new browser full window with JavaScript?

To open a PDF in a new browser full window with JavaScript, we can call the window.open method with the PDF URL.

For instance, we write:

<a href="#" >open</a>

to add a link.

Then we write:

const a = document.querySelector('a')
a.onclick = () => {
  window.open('http://www.africau.edu/images/default/sample.pdf', '_blank', 'fullscreen=yes');
}

to select the anchor element with document.querySelector.

Then we set its onclick property that calls window.open to open the PDF URL in a new window.

'_blank' will make it open in a new window.

Conclusion

To open a PDF in a new browser full window with JavaScript, we can call the window.open method with the PDF URL.

Categories
JavaScript Answers

How to detect CSS variable support with JavaScript?

Sometimes, we want to detect CSS variable support with JavaScript.

In this article, we’ll look at how to detect CSS variable support with JavaScript.

How to detect CSS variable support with JavaScript?

To detect CSS variable support with JavaScript, we can use the CSS.supports method.

For instance, we write:

console.log(CSS.supports('color', 'var(--fake-var)'))

to check if we can set the color CSS property to var(--fake-var).

If this returns true, then CSS variables is supported.

Conclusion

To detect CSS variable support with JavaScript, we can use the CSS.supports method.

Categories
JavaScript Answers

How to get the previous day with JavaScript?

Sometimes, we want to get the previous day with JavaScript.

In this article, we’ll look at how to get the previous day with JavaScript.

How to get the previous day with JavaScript?

To get the previous day with JavaScript, we can call setDate with the date minus 1.

For instance, we write:

const dateObj = new Date()
dateObj.setDate(dateObj.getDate() - 1);
console.log(dateObj)

We create a new Date instance and assigned it to dateObj.

Then we set the dateObj to the previous day by calling setDate with dateObj.getDate() - 1.

Conclusion

To get the previous day with JavaScript, we can call setDate with the date minus 1.

Categories
JavaScript Answers

How to check if any text input has value with JavaScript?

Sometimes, we want to check if any text input has value with JavaScript.

In this article, we’ll look at how to check if any text input has value with JavaScript.

How to check if any text input has value with JavaScript?

To check if any text input has value with JavaScript, we can use document.querySelectorAll to select all the inputs.

Then we can spread the node list into an array and use the array some method to check whether any element has a value set.

For instance, we write:

<input>
<input value='foo'>
<input>

to add some inputs.

Then we write:

const hasValue = [...document.querySelectorAll('input')]
  .some(el => el.value)
console.log(hasValue)

to select all the inputs with document.querySelectorAll.

Then we spread the node list into an array with the spread operator.

Then we call some to check whether any input has value set.

Therefore, hasValue should be true.

Conclusion

To check if any text input has value with JavaScript, we can use document.querySelectorAll to select all the inputs.

Then we can spread the node list into an array and use the array some method to check whether any element has a value set.