GithubHelp home page GithubHelp logo

async-single's Introduction

async-single

run an async function but only once at a time.

also, debounce the function with timers.

example

var AsyncSingle = requrie('async-single')

var as = AsyncSingle(function (value, cb) {
  fs.writeFile(filename, value, cb) //or some other async thing!
})

//write as often as you like.
//not every write will happen,
//but it will write every so often
as.write('hello world')

as.write('again!')

//when you close, it will finish off any writes!
as.close(function () {...})

exploration of async programming style, by solving the same problem the same way several times.

The problem, is to take an async function, and wrap it to that it is only called once at a time.

var async = Single(function (value, cb) {
  ThisMayTakeSomeTime(function () {
    //...
    cb()
  })
})

async.onDrain = function () {
  //called when there is no longer something waiting to write
}

async.write(value)

if you call many times quickly, it may write the first time, but then wait until that succeeds before writing the latest value. If you have three quick writes async.write(1);async.write(2);async.write(3); then the first write is fired, then if the async operation is still in flight before the others are received, the value of the last write will be performed.

(in practice, we would probably want to delay the first write so that quick writes get bundled! but we are keeping this simple as an exercise!)

License

MIT

async-single's People

Contributors

dominictarr avatar

Watchers

 avatar  avatar  avatar

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.