This package has been deprecated

Author message:

rewritten as alinex-async

alinex-once

0.1.1 • Public • Published

Package: alinex-once

Build Status Coverage Status Dependency Status

Short helper function which lets a function run only once also if called multiple times.

This is useful in event driven calls in which the callback may be called multiple times or then a function is called in parallel an have to initialize once before going on.

It is one of the modules of the Alinex Universe following the code standards defined there.

Install

The easiest way is to let npm add the module directly:

> npm install alinex-fs --save

NPM

Usage

First the library have to be included:

var once = require('alinex-once');

Now a function may be wrapped with the once method:

var fn = once.skip(function(a, b, cb) {
  return cb(null, a + b);
});

And now you can call the function as normal but on the second call it will return imediately without running the code:

fn(2, 3, function(err, x) {
  // x will now be 5
  fn(2, 3, function(err, x) {
    // err will now be set
  });
});

You may use this helper in case of initialization (wait) there a specific method have to run once before any other call can succeed. Or then events are involved and an error event will trigger the callback and the end event will do the same.

API

  • throw - throw an error if it is called a second time
  • skip - skip execution and immediately return on second call
  • atime - only run it once at a time but response all calls with the result
  • wait - the second call will get return with the same result

License

Copyright 2015 Alexander Schilling

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Readme

Keywords

none

Package Sidebar

Install

npm i alinex-once

Weekly Downloads

1

Version

0.1.1

License

Apache-2.0

Last publish

Collaborators

  • alinex