ugly-adapter

get promises out of standard callback APIs

Universal Callback => Promise Adapter

With Koa and ES7 async functions gaining prominence, the JavaScript world is poised to transition to promises. That means we need a sane way to promisify callback-based APIs. Rather than importing lots of different promisification wrappers for each API, this lib provides a single way to promisify every API.

Install

npm install ugly-adapter

Use

import adapt from 'ugly-adapter';
...
var data = await adapt(fs.readFile, './data.txt', 'utf8');

This lib also exposes methods to make partial application easier. Which is useful if you want to re-use an adapted version of a function.

var read = adapt.part(fs.readFile);
var data1 = await read('./data1', 'utf8');
var data2 = await read('./data2', 'utf8');
API

Call a bare function: adapt()

Useful when you don't think a function cares about this.

// signature
var promise = adapt(<function>, ...args)

// fs.readFile(path, enc, callback) example
adapt(fs.readFile, './data.txt', 'utf8').then(...)

Call a method on an object: adapt.method()

Useful when you think a function definitely cares about this.

// signature
var promise = adapt.method(<object>, <string>, ...args)

// user.authenticate(opts, callback) example
var user = new User()
adapt.method(user, 'authenticate', {
  userName: userName,
  password: password
}).then(...)

Partially apply a bare function: adapt.part()

// signature
var fn = adapt.part(<function>, ...args)

// example
var stat = adapt.part(fs.stat, './data.txt', 'utf8')
stat().then(...)

Partially apply a method on an object: adapt.method.part()

// signature
var fn = adapt.method.part(<object>, <string>, ...args)

// example
var user = new User()
var authenticate = adapt.method.part(user, 'authenticate')
authenticate({
  userName: userName,
  password: password
}).then(...)

A note about partial application. You can basically move the )( around willy-nilly.

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

Promify a library: adapt.promify(lib)

You can promisify entire library modules, such as fs. It will return an object with all the same properties and functions. The functions have the same signature—sans callback—and return promises.

var adapt = require('ugly-adapter')
  , callbackFs = require('fs')
  , fs = adapt.promify(callbackFs);

fs.stat(...).then(...);

The above promifies every function in the fs module. If you only want to promify a subset of functions, declare them in a whitelist.

var adapt = require('ugly-adapter')
  , callbackFs = require('fs')
  , fs = adapt.promify(callbackFs, 'stat', 'readFile');

fs.stat(...).then(...);
fs.readFile(...).then(...);
fs.link(...).then(...); // error, wasn't in the whitelist!
Async/Await Example

Now that we can convert callbacks to promises, we can write async functions everywhere.

async function jsonReadFile(path, encoding) {
  let data = await adapt(fs.readFile, encoding);
  data = JSON.parse(data);
  return data;
}
Any Promise

Ugly adapter uses any-promise promises. This is something you can safely ignore and everything works normally. However, if you want to polyfill/replace your environment's Promise for whatever reason, this allows you swap in whatever conforming Promise you want and this library will use it.

Repository

https://github.com/greim/ugly-adapter


上一篇:[Webpack]-详解create-react-app
下一篇:await

相关推荐

  • 如何使用chrome调试iphone页面?-- remotedebug-ios-webkit-adapter 安装与使用

    使用chrome devtools 调试 iphone 页面,需安装 remotedebug-ios-webkit-adapter 工具。 这个 adapter 可牛批坏了,可以通过VS Code,C...

    2 年前
  • 【30秒一个知识点】Adapter

    本系列翻译自开源项目 30-seconds-of-code 这是一个非常优秀的系列,文章总结了大量的使用es6语法实现的代码模块不是说真的三十秒就能理解,也需要你认真的思考,其中有一些点非常精妙,...

    2 年前
  • webrtc-adapter-test

    Hide browser differences in WebRTC APIs (test package name) WebRTC adapter [adapter.js] is a shim t...

    1 年前
  • webrtc-adapter

    A shim to insulate apps from WebRTC spec changes and browser prefix differences WebRTC adapter adap...

    2 年前
  • waterline-adapter-tests

    Integration tests for waterline adapters Waterline Adapter Tests A set of integration tests that ...

    1 年前
  • vue2.0+axios+mock+axios-mock+adapter实现登陆

    做项目过程中,需要用到后台模拟数据,在机缘巧合下发现了mock,在学习使用的过程中又偶遇了axios-mock-adapter。现在将实例展示如下: 准备 实例是建立在vue-cli的基础上实现 ...

    2 年前
  • vue-jscodeshift-adapter

    Run jscodeshift on Vue single file components vue-jscodeshift-adapter Run jscodeshift on Vue singl...

    3 个月前
  • vscode-debugadapter

    Debug adapter implementation for node VS Code Debug Adapter Npm module to implement a VS Code debu...

    1 年前
  • unexpected-htmllike-testrenderer-adapter

    Adapter for unexpected-htmllike for react-test-renderer unexpected-htmllike-testrenderer-adapter Thi...

    2 年前
  • unexpected-htmllike-reactrendered-adapter

    undefined unexpected-htmllike-reactrendered-adapter CDN by jsDelivr...

    3 个月前

官方社区

扫码加入 JavaScript 社区