kew: Lightweight Promise Library

0.7.0 · abandoned · verified Tue Apr 21

kew is a lightweight promise/deferred library specifically designed for Node.js environments. Developed by Medium, its primary goal was to offer a high-performance alternative to the Q library, particularly for server-side applications experiencing CPU bottlenecks in chained database callbacks. Currently at version 0.7.0, the package appears to be unmaintained, with its last commit dating back several years, indicating an abandoned status and no active release cadence. It provides a subset of promise functionality, including methods like `Q.resolve()`, `Q.reject()`, `Q.defer()`, and promise chaining with `.then()` and `.fail()`. Unlike more comprehensive promise libraries, kew intentionally implements only the features necessary for the common server-side use cases identified by its original developers, prioritizing minimal footprint and speed.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `Q.resolve()`, `Q.defer()`, handling deferred promises, and using `makeNodeResolver()` to integrate with Node.js-style callbacks, including error handling.

const Q = require('kew');

// 1. Create a promise from a literal value
const successPromise = Q.resolve('Hello from kew!');

successPromise.then(function(message) {
  console.log('Resolved with:', message);
});

// 2. Create a deferred promise and resolve it later
const myDeferred = Q.defer();

setTimeout(function() {
  myDeferred.resolve('Delayed success!');
}, 500);

myDeferred.promise.then(function(result) {
  console.log('Deferred resolved with:', result);
});

// 3. Handle node-style callbacks with makeNodeResolver
function getNodeStyleData(id, callback) {
  setTimeout(() => {
    if (id === 1) {
      callback(null, { id: 1, name: 'Item 1' });
    } else {
      callback(new Error('Item not found!'));
    }
  }, 200);
}

const dataDefer = Q.defer();
getNodeStyleData(1, dataDefer.makeNodeResolver());

dataDefer.promise
  .then(function(obj) {
    console.log('Node-style callback success:', obj);
  })
  .fail(function(e) {
    console.error('Node-style callback error:', e.message);
  });

const errorDefer = Q.defer();
getNodeStyleData(2, errorDefer.makeNodeResolver());

errorDefer.promise
  .then(function(obj) {
    console.log('Node-style callback success (should not happen):', obj);
  })
  .fail(function(e) {
    console.error('Node-style callback error (expected):', e.message);
  });

view raw JSON →