包详细信息

2-thenable

medikoo3mISC1.0.0

Convert any object to thenable

async, promise, thenable, then

自述文件

*nix build status Windows build status Tests coverage Transpilation status

2-thenable

Convert object into a thenable

Useful when we want to imply an asynchronous representation onto some non-promise object.

Having that target object can be combined into promise chains or async/await syntax. One use case would be turning a stream instance so it's also a promise.

Installation

npm install 2-thenable

How it works

Utility takes target and promise arguments. target is object to be extended with then, catch and finally methods. While promise is expected to be a native promise instance that reflects resolution which should be mapped onto target

Usage

Example of converting stream to thenable

const toThenable = require("2-thenable");

// Example of converting a simple utf8 string stream to thenable
toThenable(stream, new Promise((resolve, reject) => {
    let result = '';
    stream.on('error', reject);
    stream.on('data', data => (result += data));
    stream.on('end', () => resolve(result));
});

stream.then(result => {
    console.log("Cumulated string data", result);
});

Tests

npm test

更新日志

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.0.0 (2019-01-16)