JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2113
  • Score
    100M100P100Q112934F
  • License MIT

get promises out of standard callback APIs

Package Exports

  • ugly-adapter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ugly-adapter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Straightforward way to obtain promises from your standard error-first callback functions

Large swaths of the npm ecosystem—plus node.js and io.js—have async APIs that accept callbacks. Many people wish these APIs produced promises instead. Whether or not that wish is someday fulfilled, this lib provides an easy way to obtain promises from error-first callback-driven APIs. No heavy infrastructure here, just a simple adapter.

Install

npm install ugly-adapter

Use

var adapt = require('ugly-adapter')
  , promise = adapt(fs.readFile, './data.txt', 'utf8')
// helper for partial application
var read = adapt.part(fs.readFile)
  , promise = read('./data.txt', 'utf8')

API

Call a bare function

var adapt = require('ugly-adapter')
  , promise = adapt(someFunction, ...args)

Call a method on an object

// method sees proper 'this'
var adapt = require('ugly-adapter')
  , promise = adapt.method(object, methodName, ...args)

Partially apply a bare function

var adapt = require('ugly-adapter')
  , fn = adapt.part(someFunction, ...someArgs)
  , promise = fn(...someMoreArgs)

Partially apply a method on an object

var adapt = require('ugly-adapter')
  , fn = adapt.method.part(object, methodName, ...someArgs)
  , promise = fn(...someMoreArgs)

Note about partial application. You can basically just move the )( around willy-nilly.

// these behave identically
var promise = adapt.part(a, b)()
var promise = adapt.part(a)(b)
var promise = adapt.part()(a, b)

FAQ

  1. Aren't promises slow? To satisfy the promises-are-slow-you-should-use-bluebird crowd, this lib will create and return native-or-bluebird promises. Either way, the API is identical. Note that this lib doesn't actually pull in or depend on Bluebird in any way. You must depend on it in your own package.json. There's a benchmark in this project which sucks and should be improved, but which you can run if you want.
  2. Doesn't Lib X already do this? Yes, and lots of other stuff besides. This lib is very minimal. If you're already using Lib X, you should probably keep using it and ignore this. If, however, you're using a generator trampoline like co and all you want are yieldables, maybe this is the ticket.
  3. Is es6 required? Sort of. Bluebird (see above) or a global Promise constructor (which is part of es6) is required. If neither are found this lib will only bring cataclysm and death.