Giter Club home page Giter Club logo

knockout-async-computed's Introduction

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

knockout-async-computed's People

Contributors

macco3k avatar stephenjjbrown avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

macco3k

knockout-async-computed's Issues

Implement disposal logic

This works well until it comes to disposing of the computed and the subscriptions. A disposal function must be attached to the return value to get rid of any subscriptions and the computed itself

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.