├── .gitignore ├── index.js ├── test.js ├── package.json ├── readme.md ├── index.d.ts └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | shrinkwrap.yaml 3 | package-lock.json 4 | yarn.lock -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function run(fn) { 2 | return (...args) => 3 | new Promise(resolve => { 4 | fn(...args, (err, res) => resolve([err, res])); 5 | }); 6 | }; 7 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('tape'); 2 | const run = require('./index'); 3 | const fs = require('fs'); 4 | 5 | test('it can be used fs.readFile', async t => { 6 | t.plan(3); 7 | const [err, val] = await run(fs.readFile)('./test.js'); 8 | t.error(err); 9 | t.ok(val); 10 | t.ok(val.length > 10); 11 | }); 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promisify-tuple", 3 | "version": "1.2.0", 4 | "description": "Promisify a callback-style function, always resolve with [err,val]", 5 | "repository": "https://github.com/staltz/promisify-tuple", 6 | "main": "index.js", 7 | "types": "index.d.ts", 8 | "typings": "index.d.ts", 9 | "author": "Andre Staltz ", 10 | "license": "MIT", 11 | "keywords": [ 12 | "chain", 13 | "pipe", 14 | "functions", 15 | "functional", 16 | "compose" 17 | ], 18 | "engines": { 19 | "node": ">=6" 20 | }, 21 | "scripts": { 22 | "test": "tape test.js | tap-spec" 23 | }, 24 | "devDependencies": { 25 | "tap-spec": "^5.0.0", 26 | "tape": "5.2.2", 27 | "typescript": "4.2.x" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # promisify-tuple 2 | 3 | Convert a Node.js-style callback API `(err, val) => void` to a Promise that resolves to a `[err, val]` array. 4 | 5 | ``` 6 | npm install --save promisify-tuple 7 | ``` 8 | 9 | **Before:** 10 | 11 | ```js 12 | const fs = require('fs') 13 | 14 | function main() { 15 | fs.readFile('./test.js', (err, val) => { 16 | if (err) console.error(err) 17 | else console.log(val) 18 | }) 19 | } 20 | 21 | main() 22 | ``` 23 | 24 | **After:** 25 | 26 | ```js 27 | const fs = require('fs') 28 | const run = require('promisify-tuple') 29 | 30 | async function main() { 31 | const [err, val] = await run(fs.readFile)('./test.js') 32 | if (err) console.error(err) 33 | else console.log(val) 34 | } 35 | 36 | main() 37 | ``` 38 | 39 | ## License 40 | 41 | [MIT](LICENSE) 42 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | interface CB { 2 | (err?: any, x?: T): void; 3 | } 4 | declare type CBAPI = 5 | | ((a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, a9: any, cb: CB) => any) 6 | | ((a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, a8: any, cb: CB) => any) 7 | | ((a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, a7: any, cb: CB) => any) 8 | | ((a1: any, a2: any, a3: any, a4: any, a5: any, a6: any, cb: CB) => any) 9 | | ((a1: any, a2: any, a3: any, a4: any, a5: any, cb: CB) => any) 10 | | ((a1: any, a2: any, a3: any, a4: any, cb: CB) => any) 11 | | ((a1: any, a2: any, a3: any, cb: CB) => any) 12 | | ((a1: any, a2: any, cb: CB) => any) 13 | | ((a1: any, cb: CB) => any) 14 | | ((cb: CB) => any) 15 | declare function run(api: CBAPI): (...args: Array) => Promise<[any, T]>; 16 | export = run; 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 André Staltz (staltz.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------