Socket
Socket
Sign inDemoInstall

z-core

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    z-core

Utility library for JavaScript promises


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Install size
82.0 kB
Created
Weekly downloads
 

Readme

Source

Z Build Status

Utility library for JavaScript promises

Test status

Selenium Test Status

It's also been tested in Node.js 0.10 and 0.11.

Installation

Use npm: npm install z-core and then var Z = require('z-core');

Or bower: (not uploaded yet)

Or download it manually from the dist folder of this repo.

Wrapping functions to accept promises as parameters and return promises

Use bindSync to create promise-friendly functions from sync functions.

var pmin = Z.bindSync(Math.min);

// It can still be called with regular values
pmin(10, 5).then(function(minValue) {
  console.log(minValue); // 5
});

// But is can also be called with promises
var promise = returnsTheValue2AsPromise();
pmin(promise, 5).then(function(minValue) {
  console.log(minValue); // 2
});

Use bindAsync to create promise-friendly functions from async functions.

var postPromisedJSON = Z.bindAsync(postJSON);
var agePromise = returnsTheValue28AsPromise();

// Note that it's called with a mix of regular values an promises
postPromisedJSON('/people', { name: 'Jakob', age: agePromise }).then(function(res) {
  console.log(res); // the result of the request
});

Augmenting promises

Extend the promises returned by Z using mixins.

Z.mixin({
  get: function(prop) {
    return this.value[prop];
  },
  toLowerCase: function() {
    return this.value.toLowerCase();
  },
  first: function(n) {
    if (n == null) {
      n = 1;
    }
    return this.value.slice(0, n);
  },
  log: function() {
    console.log(this.value);
  }
});

var getPromisedJSON = Z.bindAsync(getJSON);

var firstThreeInName = getPromisedJSON('/cookies/123').get('name').toLowerCase().first(3);

firstThreeInName.log();

Use a prepackaged version of Z

There are several mixin packages available for your convenience:

And even bundles where certain set of mixins have already been applied:

  • z-std-pack: Z, builtins and underscore bundled together.

Additional resources

Slides from 2014-02-25 presentation on Z

Code from the above presentation

Keywords

FAQs

Last updated on 16 Mar 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc