Categories
JavaScript Answers

How to call an async function within a Promise .then with JavaScript?

Sometimes, we want to call an async function within a Promise .then with JavaScript.

In this article, we’ll look at how to call an async function within a Promise .then with JavaScript.

How to call an async function within a Promise .then with JavaScript?

To call an async function within a Promise .then with JavaScript, we can return the result of the async function call in the then callback.

For instance, we write:

const f = async () => 'foo'

Promise.resolve(1)
  .then((res) => {
    console.log(res)
    return f()
  })
  .then((res) => {
    console.log(res)
  })

to create the f async function.

Then we call f in the then callback to trigger the call of the 2nd then callback which logs the resolved value of the promise returned by f.

Therefore, we see

1
"foo"

logged.

Conclusion

To call an async function within a Promise .then with JavaScript, we can return the result of the async function call in the then callback.

Categories
JavaScript Answers

How to use JavaScript to create a client side email?

Sometimes, we want to use JavaScript to create a client side email.

In this article, we’ll look at how to use JavaScript to create a client side email.

How to use JavaScript to create a client side email?

To use JavaScript to create a client side email, we can create a mailto URL.

For instance, we write:

const addresses = "foo@example.com";
const body = "hello"
const subject = "subject"
const href = `mailto:${addresses}?subject=${subject}&body=${body}`
const wndMail = window.open(href, "_blank", "scrollbars=yes,resizable=yes,width=10,height=10");

to create the href mailto URL.

We combine the addresses to send to, email subject and email body into one string.

And then we call window.open with href to open the URL, which opens the default mail client with the compose email window.

Conclusion

To use JavaScript to create a client side email, we can create a mailto URL.

Categories
JavaScript Answers

How to compare two variables containing strings in JavaScript?

Sometimes, we want to compare two variables containing strings in JavaScript.

In this article, we’ll look at how to compare two variables containing strings in JavaScript.

How to compare two variables containing strings in JavaScript?

To compare two variables containing strings in JavaScript, we can use the === operator.

For instance, we write:

const a = 'foo'
const b = 'foo'
console.log(a === b)

to compare strings a and b with === to check if they’re equal.

Therefore, the console log logs true since they’re the same.

Conclusion

To compare two variables containing strings in JavaScript, we can use the === operator.

Categories
JavaScript Answers

How to fill rectangle with pattern with JavaScript?

Sometimes, we want to fill rectangle with pattern with JavaScript.

In this article, we’ll look at how to fill rectangle with pattern with JavaScript.

How to fill rectangle with pattern with JavaScript?

To fill rectangle with pattern with JavaScript, we can use d3.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.3.0/d3.min.js"></script>

to add the d3 script.

Then we write:

const svg = d3.select("body").append("svg");

svg
  .append('defs')
  .append('pattern')
  .attr('id', 'diagonalHatch')
  .attr('patternUnits', 'userSpaceOnUse')
  .attr('width', 4)
  .attr('height', 4)
  .append('path')
  .attr('d', 'M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2')
  .attr('stroke', '#000000')
  .attr('stroke-width', 1);

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .style("fill", 'orange');

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .attr('fill', 'url(#diagonalHatch)');

to create an svg element and append it to the body as its child with

const svg = d3.select("body").append("svg");

Then we write

svg
  .append('defs')
  .append('pattern')
  .attr('id', 'diagonalHatch')
  .attr('patternUnits', 'userSpaceOnUse')
  .attr('width', 4)
  .attr('height', 4)
  .append('path')
  .attr('d', 'M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2')
  .attr('stroke', '#000000')
  .attr('stroke-width', 1);

to add the diagonal hatch pattern.

We add the width, height stroke, stroke-width, etc with attr.

We set the 'id' of the pattern so we can apply it later by referencing its ID.

Next, we add the rectangle with

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .style("fill", 'orange');

And we set width, height and fill of the rectangle with attr.

Finally, we append the diagonal hatch pattern we created with

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .attr('fill', 'url(#diagonalHatch)');

We call attr with 'fill' and 'url(#diagonalHatch)' to apply the diagonalHatch pattern we created earlier.

Conclusion

To fill rectangle with pattern with JavaScript, we can use d3.

Categories
JavaScript Answers

How to detect if microphone permissions have been granted in Chrome with JavaScript?

Sometimes, we want to detect if microphone permissions have been granted in Chrome with JavaScript.

In this article, we’ll look at how to detect if microphone permissions have been granted in Chrome with JavaScript.

How to detect if microphone permissions have been granted in Chrome with JavaScript?

To detect if microphone permissions have been granted in Chrome with JavaScript, we can use the navigation.permissions.query method.

For instance, we write:

const checkMic = async () => {
  const permissionStatus = await navigator.permissions.query({
    name: 'microphone'
  })
  console.log(permissionStatus)
}
checkMic()

to define the checkMic function that calls navigation.permissions.query with { name: 'microphone' } to return a promise with the microphone permission data.

As a result, we get something like

{name: 'audio_capture', state: 'denied', onchange: null}

logged.

Conclusion

To detect if microphone permissions have been granted in Chrome with JavaScript, we can use the navigation.permissions.query method.