1.0.4 • Published 5 years ago

vue-prom v1.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

vue-prom

Vue promise wrapper component

About

The goal of this component is to simplify rendering logic based on promise state (pending / fulfilled, rejected). The component keeps track of the promise's state and proposes slots to render content accordingly.

You should avoid this component when:

  • You need to store the result of the promise (i.e to send it back later or if it is required for a computed property).
  • You need to make a function call inside of the promise callback (i.e store the result in Vuex or trigger your notification library).

Installation

NPM

npm install --save vue-prom

npm package link

CDN

<script src="https://unpkg.com/vue-prom@latest"></script>

Usage

With vue-prom we would write the following:

<template>
  <div>
      <vue-prom :promise="api.getUser()">
          <div slot="pending">
              Loading user...
          </div>
          <div slot="then" slot-scope="{result}">
              Hello {{ result.firstName }} {{ result.lastName }}
          </div>
          <div slot="catch" slot-scope="{error}">
              {{ error.message }}
          </div>
      </vue-prom>
  </div>
</template>

<script>
import VueProm from 'vue-prom';
import api from './api';
export default {
  data() {
    api
  },
  components: {
      VueProm
  }
};
</script>

Instead of:

<template>
    <div>
        <div v-if="loading">
            Loading user...
        </div>
        <div v-else-if="error">
            {{ error.message }}
        </div>
        <div v-else>
            Hello {{ result.firstName }} {{ result.lastName }}
        </div>
    </div>
</template>

<script>
import api from './api';
export default {
  created() {
    this.loading = true;
    this.error = null;
    api
      .getUser()
      .then(result => (this.user = result))
      .catch(error => (this.error = error))
      .finally(_ => (this.loading = false));
  },
  data() {
    return {
      loading: false,
      user: null
    };
  }
};
</script>

Alternatively, to keep the template concise, we can omit the 'pending' and 'catch' slots altogether and rely on the default labels provided by the component instead.

<template>
  <div>
      <vue-prom :promise="api.getUser()">
          <div slot="then" slot-scope="{result}">
              Hello {{ result.firstName }} {{ result.lastName }}
          </div>
      </vue-prom>
  </div>
</template>

Props

  • promise: required, the promise to resolve.
  • refresh: refresh trigger.

The component watches both the promise and refresh props, the promise will automatically re-execute when the value of either of these changes.

Slots

All slots are optional.

NameVisible whenSlot type(s)If absent
pendingThe promise is pendingRegular onlyA span with 'Loading...'
catchThe promise was rejectedRegular and scopedA span with 'Error'
thenThe promise was fulfilledRegular and scopedA span with 'Loaded'

Data exposed by scoped slots:

  • Scoped 'catch' slot exposes the 'error' object.
  • Scoped 'then' slot exposes the 'result' object.
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago