promiser.js
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "promiser.js", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Creates a native ES6 Promise from a function which takes a node style Error-First callback.", | ||
@@ -5,0 +5,0 @@ "main": "promiser.js", |
@@ -1,41 +0,53 @@ | ||
const createPromise = require('./promiser.js'); | ||
# promiser.js | ||
Create native ES6 promises from functions which take node-style callbacks. | ||
// How to create promises from functions. | ||
const fs = require('fs'); | ||
## How to use | ||
createPromise(fs.readFile)('test.js', 'utf8'). | ||
then(() => console.log('SUCCESSFUL SUCCESS (function)')). | ||
catch(() => { throw new Error('FAILED SUCCESS (function)');}); | ||
### require the library: | ||
createPromise(fs.readFile)('non-existent-file', 'utf8'). | ||
then(() => { throw new Error('FAILED FAILURE (function)');}). | ||
catch(() => console.log('SUCCESSFUL FAILURE (function)')); | ||
const createPromise = require('./promiser.js'); | ||
// How to create promise from methods. | ||
class TestClass { | ||
constructor(successStr, failStr, success) { | ||
this.failStr = failStr; | ||
this.successStr = successStr; | ||
this.success = success; | ||
} | ||
a(errFirstCallBack) { | ||
if (this.success) { | ||
errFirstCallBack(null, this.successStr); | ||
} else { | ||
errFirstCallBack(this.failStr, null); | ||
} | ||
} | ||
} | ||
### How to create promises from functions. | ||
const fs = require('fs'); | ||
createPromise(fs.readFile)('test.js', 'utf8'). | ||
then(() => console.log('SUCCESSFUL SUCCESS (function)')). | ||
catch(() => { throw new Error('FAILED SUCCESS (function)');}); | ||
const testClassSuccess = new TestClass('SUCCESSFUL SUCCESS (method)', 'FAILED SUCCESS (method)', true); | ||
createPromise(fs.readFile)('non-existent-file', 'utf8'). | ||
then(() => { throw new Error('FAILED FAILURE (function)');}). | ||
catch(() => console.log('SUCCESSFUL FAILURE (function)')); | ||
// Remember to pass context to createPromise | ||
createPromise(testClassSuccess.a, testClassSuccess)(). | ||
then(console.log). | ||
catch(console.error); | ||
### How to create promise from methods. | ||
const testClassFailure = new TestClass('FAILED FAILURE (method)', 'SUCCESSFUL FAILURE (method)', false); | ||
Assuming we have a class which has a prototype function, `a`, which takes a node-style callback: | ||
createPromise(testClassFailure.a, testClassFailure)(). | ||
then(console.log). | ||
catch(console.error); | ||
class TestClass { | ||
constructor(successStr, failStr, success) { | ||
this.failStr = failStr; | ||
this.successStr = successStr; | ||
this.success = success; | ||
} | ||
a(errFirstCallBack) { | ||
if (this.success) { | ||
errFirstCallBack(null, this.successStr); | ||
} else { | ||
errFirstCallBack(this.failStr, null); | ||
} | ||
} | ||
} | ||
Instantiate the class, then create a promise from the method `a` by passing it to promiser, | ||
just don't forget to pass the context aswell! | ||
const testClassSuccess = new TestClass('SUCCESSFUL SUCCESS (method)', 'FAILED SUCCESS (method)', true); | ||
createPromise(testClassSuccess.a, testClassSuccess)(). | ||
then(console.log). | ||
catch(console.error); | ||
const testClassFailure = new TestClass('FAILED FAILURE (method)', 'SUCCESSFUL FAILURE (method)', false); | ||
createPromise(testClassFailure.a, testClassFailure)(). | ||
then(console.log). | ||
catch(console.error); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13706
54