Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promiser.js

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promiser.js - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

2

package.json
{
"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);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc