Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
/ only-last Public archive

Zero dependency helper for resolve race conditions on user input

Notifications You must be signed in to change notification settings

project-cemetery/only-last

Repository files navigation

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.

About

Zero dependency helper for resolve race conditions on user input

Resources

Stars

Watchers

Forks

Packages

No packages published