Categories
JavaScript Answers

How to chain promises with promises inside then() with JavaScript?

Sometimes, we want to chain promises with promises inside then() with JavaScript.

In this article, we’ll look at how to chain promises with promises inside then() with JavaScript.

How to chain promises with promises inside then() with JavaScript?

To chain promises with promises inside then() with JavaScript, we can return a promise in the then callback.

For instance, we write:

Promise
  .resolve(1)
  .then((res) => {
    console.log(res)
    return Promise.resolve(2)
  })
  .then((res) => {
    console.log(res)
    return Promise.resolve(3)
  })
  .then((res) => {
    console.log(res)
  })
  .catch((e) => {
    console.error(e);
  });

to call then with callbacks that returns another promise.

Promise.resolve returns a promise.

res has the resolved value of the previous promise.

Each then callback will run in the same order they’re listed.

Therefore, we see

1
2
3

logged.

Conclusion

To chain promises with promises inside then() with JavaScript, we can return a promise in the then callback.

Categories
JavaScript Answers

How to disable an input field using JavaScript?

Sometimes, we want to disable an input field using JavaScript.

In this article, we’ll look at how to disable an input field using JavaScript.

How to disable an input field using JavaScript?

To disable an input field using JavaScript, we can set the disabled property of the input to true.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.disabled = true

to select an input with querySelector.

And then we set input.disabled to true to disable the input.

Conclusion

To disable an input field using JavaScript, we can set the disabled property of the input to true.

Categories
JavaScript Answers

How to delete cookie in Express and JavaScript?

Sometimes, we want to delete cookie in Express and JavaScript.

In this article, we’ll look at how to delete cookie in Express and JavaScript.

How to delete cookie in Express and JavaScript?

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.get('/logout', (req, res) => {
  res.clearCookie('cookie');
  return res.sendStatus(200);
})


app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to call res.clearCookie with the key of the cookie to delete in the /logout endpoint.

Conclusion

To delete cookie in Express and JavaScript, we can use the res.clearCookie method.

Categories
JavaScript Answers Vue Vue Answers

How to invert boolean inline on click with Vue.js and JavaScript?

Sometimes, we want to invert boolean inline on click with Vue.js and JavaScript.

In this article, we’ll look at how to invert boolean inline on click with Vue.js and JavaScript.

How to invert boolean inline on click with Vue.js and JavaScript?

To invert boolean inline on click with Vue.js and JavaScript, we can set the @click directive to a statement to toggle the boolean.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div>
      <button @click='toggle = !toggle'>
        {{ toggle ? 'show' : 'hide' }}
      </button>
    </div>
  `,
  data: {
    toggle: true
  },
})

to set @click to toggle = !toggle to toggle the toggle reactive property.

And we display show' if toggleistrue` and ‘hide’ otherwise.

Therefore, when we click the button, we see the button text switch between ‘show’ and ‘hide’.

Conclusion

To invert boolean inline on click with Vue.js and JavaScript, we can set the @click directive to a statement to toggle the boolean.

Categories
JavaScript Answers

How to select multiple options with JavaScript?

Sometimes, we want to select multiple options with JavaScript.

In this article, we’ll look at how to select multiple options with JavaScript.

How to select multiple options with JavaScript?

To select multiple options with JavaScript, we can set the selected property of the option we want to select to true.

For instance, we write:

<select multiple>
  <option value="1">One</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>

to add a multi-select element.

Then we write:

const select = document.querySelector('select')
for (const o of select.options) {
  if (+o.value < 3) {
    o.selected = true
  }
}

to select the select element with querySelector.

Then we loop through the options in the element with the for-of loop.

We get the options with select.options.

And if o.value is less than 3, we set o.selected to true.

Therefore, the first 2 choices are selected since their value attributes have value less than 3 when converted to a number.

Conclusion

To select multiple options with JavaScript, we can set the selected property of the option we want to select to true.