1.2.0 • Published 1 year ago

knockout-async-computed v1.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
1 year ago

Build Status codecov Wallaby.js

Knockout Async Computed

Allows you to easily create computed observables from async functions or Promises.

Dependencies between async computeds are automatically tracked by Knockout, just like any other computed. If an older async operation takes longer than a more recent one, the older one is automatically cancelled (the Promise rejected), ensuring you always get the most current value, and at the correct time.

Combine this library with a memoize library to reduce repeat network calls to the same resource.

Usage

As an extender

// Adds the extender to ko.extenders
registerAsyncComputed(ko)

// Pass async function or regular function that returns Promise
const items = ko.computed(async () => {

   // Do something here
   return fetch("/api/entities.json")
      .then(r => r.json())
      
}).extend({async: []}) //  Provide initial value. This is used until the async function is completed for the first time

As a factory (recommended for Typescript)

import { asyncComputed } from "knockout-async-computed"

// Create using factory function
// Pass async function or regular function that returns Promise
const items = asyncComputed(async () => {

   // Do something here
   return fetch("/api/entities.json")
      .then(r => r.json())
      
}, []) // Provide initial value as 2nd argument

Build

If you wish to compile the module yourself:

Run npm install to download dependencies, then run npm run build

Run npm test to run tests.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

1.2.0

1 year ago

1.1.0

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago