1.1.3 • Published 5 years ago

only-last v1.1.3

Weekly downloads
18
License
MIT
Repository
-
Last release
5 years ago

only-last

Tiny (less than 1kb), zero dependency helper for race conditions on user input.

TL;DR

import { OnlyLast } from 'only-last'

const executor = new OnlyLast()

const search = async (query) => {
  const result = await executor.execute(() => api.getData(query))

  return result.data
}

// in other place

search('he').then(data => {
  // data is a result of search 'hello'
})

// wait 40 ms

search('hello').then(data => {
  // data is a result of search 'hello'
})

Installation

yarn add only-last

Or if you prefer npm:

npm i only-last

Usage

You should create new executor for every function, which you want to wrap.

import { OnlyLast } from 'only-last'

const executorForSearch = new OnlyLast()
const executorForFilter = new OnlyLast()
// etc.

Then, just use executor in your function:

const search = (query) =>
  executor.execute(() => {
    // do something async and really long...
  })

All race conditions on user input solved, you are beatiful!

One more thing

It is really tiny library, only 926 bytes. We use size-limit to control size.