From 6866773a99d3f823c66f8b538b0a6b9036f670f2 Mon Sep 17 00:00:00 2001 From: sturcotte Date: Sun, 19 Mar 2017 04:20:50 -0400 Subject: [PATCH] Initial implementation --- .npmignore | 5 +++++ index.ts | 31 +++++++++++++++++++++++++++++++ package.json | 22 ++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 .npmignore create mode 100644 index.ts create mode 100644 package.json diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..1c39457 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +.vscode +node_modules +**/*.ts +tsconfig.json +tslint.json \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..cdfd09d --- /dev/null +++ b/index.ts @@ -0,0 +1,31 @@ +const es6Promisify = require('es6-promisify'); + +/** + * Return a promise that resolves once the given event emitter emits the specified event + * + * @param {NodeJS.EventEmitter} emitter - The event emitter to watch + * @param {string} event - The event to watch + * @returns {Promise<{}>} - The promise that resolves once the given emitter emits the specified evnet + */ +export function once(emitter: NodeJS.EventEmitter, event: string) { + return new Promise((resolve) => { + emitter.once(event, result => { + resolve(result); + }); + }); +} + +/** + * Transform callback-based function -- func(arg1, arg2 .. argN, callback) -- into + * an ES6-compatible Promise. Promisify provides a default callback of the form (error, result) + * and rejects when `error` is truthy. You can also supply settings object as the second argument. + * + * @param {function} original - The function to promisify + * @param {object} [settings] - Settings object + * @param {object} settings.thisArg - A `this` context to use. If not set, assume `settings` _is_ `thisArg` + * @param {bool} settings.multiArgs - Should multiple arguments be returned as an array? + * @returns {function} A promisified version of `original` + */ +export function promisify(original: Function, settings?: Object): Function { + return es6Promisify(original, settings); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..d843f42 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "mysah", + "version": "0.1.0", + "description": "ES6 Promise utility belt", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "ES6", + "Promise", + "utility" + ], + "author": "Sami Turcotte", + "license": "MIT", + "devDependencies": { + "@types/node": "^7.0.8" + }, + "dependencies": { + "es6-promisify": "^5.0.0" + } +}