Socket
Book a DemoInstallSign in
Socket

async-source

Package Overview
Dependencies
Maintainers
3
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-source

async requests wrapper

1.1.12
latest
Source
npmnpm
Version published
Weekly downloads
567
139.24%
Maintainers
3
Weekly downloads
 
Created
Source

AsyncSource

With AsyncSource you can

  • Create stateful reactive data source for asynchronous requests
  • Handle errors
  • Configure throttling
  • Ignore inconsistent state
  • Control state of your requests (isLoading, isFetch, data)

In case of multiple calls - only last call will be processed. Connect AsyncSource to your dynamic selects with in build debounce

Install

npm install --save async-source

Usage

// javascript
import AsyncSource from 'async-source';

const source = new AsyncSource(request);

async function loadItems() {
    await source.update(...requestParams);
    const response = source.data;
}
// typescript
import AsyncSource from 'async-source';

const source = new AsyncSource<RespnonsType>(request);

async function loadItems() {
    await source.update(...requestParams);
    const response = source.data;
}

Parameters

ParameterIs requiredDescription
serviceMethodtrueMethod that returns promise
errorHandlerfalseFunction that will be called in case of service method rejected
delayfalsedelay in ms

Properties(reactive getters)

PropertyDescriptionType
dataReturns your method responseYour method response
isLoadingReturns true when request pendingBoolean
isFetchReturns true after first loadBoolean

Methods

MethodDescriptionParams
updateCalls be requestYour method request params
pushCalls be request and handles success responseSuccess handler, Your method request params
updateIfEmptyCalls be request in source if data is emptyYour method request params
updateOnceCalls be request in source if data is empty and waits till other request resolvedYour method request params
updateImmediateCalls be request in source ignoring debounceYour method request params
clearSet source data to initial state(null)---

Usage with vue3(composition api)

<template>
    <ul v-loading="isLoading">
        <li v-for="item in items" :key="item.id">
            {{ item.name }}
        </li>
    </ul>
</template>

<script>
import AsyncSource from 'async-source';
import { computed, reactive } from 'vue';

export default {
    name: 'MyComponent',
    setup() {
        function errorHandler(error) {
            console.error(error);
        }

        const source = reactive(new AsyncSource(request, errorHandler, 300));

        source.update();

        const items = computed(() => source.data || []);
        const isLoading = computed(() => source.isLoading);
        
        return {
            items,
            isLoading
        };
    }
}

</script>

Usage with vue2(option api)

<template>
    <ul v-loading="isLoading">
        <li v-for="item in items" :key="item.id">
            {{ item.name }}
        </li>
    </ul>
</template>

<script>
import AsyncSource from 'async-source';

export default {
    name: 'MyComponent',
    data() {
        return {
            source: new AsyncSource(request, this.errorHandler, 300)
        }
    },
    computed: {
        items() {
            return this.source.data || [];
        },
        isLoading() {
            return this.source.isLoading;
        }
    },
    created() {
        this.source.update();
    },
    methods: {
        errorHandler(error) {
            console.error(error);
        }
    }
}

</script>

Keywords

async

FAQs

Package last updated on 29 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.