Coder Social home page Coder Social logo

msaaddev / promise-it Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 0.0 248 KB

A simple Node.js package that helps you not to look up JavaScript promise syntax every time you use it.

License: MIT License

JavaScript 100.00%
promise promisify nodejs-promise

promise-it's Introduction

cover


A simple Node.js package that helps you not to look up JavaScript promise syntax every time you use it.

separator

  • Simple: Adds abstraction to the promise syntax
  • Easy to use: No need to learn JavaScript promise syntax
  • Promise: Turns any function into a promise
  • All promises: Provides a way to chain all promises and returns the result collectively
  • Any promise: Takes array of promise and returns a promise that resolves when any of the promise is resolved
  • All settled promises: Takes array of promise and returns a promise that resolves when all promises are settled

Install

# install the package
npm install @msaaddev/promise-it

API

promiseIt()

Promisify a callback function

promiseItAll()

Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

promiseItAny()

Promisify any of the given callback functions

promiseItAllSettled()

Creates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.


Usage

  • promiseIt()
const { promiseIt } = require('@msaaddev/promise-it');

(async () => {
	const callback = (resolve, reject) => {
		exec(`networkQuality`, error => {
			if (error) {
				handleError(error);
				reject(error);
			}
			resolve('All good. Ran networkQuality.');
		});
	};

	try {
		const promise = await promiseIt(callback);
		console.log(promise);
	} catch (err) {
		console.log(err);
	}
})();
  • promiseItAll()
const { promiseItAll } = require('@msaaddev/promise-it');
const { exec } = require('child_process');

(async () => {
	const callback = (resolve, reject) => {
		exec(`networkQuality`, error => {
			if (error) {
				handleError(error);
				reject(error);
			}
			resolve('All good. Ran networkQuality.');
		});
	};

	const callback2 = (resolve, reject) => {
		exec(`networkQuality -v`, error => {
			if (error) {
				reject(error);
			}
			resolve('All good. Ran networkQuality -v.');
		});
	};

	try {
		const allPromises = await promiseItAll(callback, callback2);
		console.log(allPromises);
	} catch (err) {
		console.log(err);
	}
})();
  • promiseItAny()
const { promiseItAny } = require('@msaaddev/promise-it');
const { exec } = require('child_process');

(async () => {
	const callback = (resolve, reject) => {
		exec(`networkQuality`, error => {
			if (error) {
				handleError(error);
				reject(error);
			}
			resolve('All good. Ran networkQuality.');
		});
	};

	const callback2 = (resolve, reject) => {
		exec(`networkQuality -v`, error => {
			if (error) {
				reject(error);
			}
			resolve('All good. Ran networkQuality -v.');
		});
	};

	try {
		const anyPromise = await promiseItAny(callback, callback2);
		console.log(anyPromise);
	} catch (err) {
		console.log(err);
	}
})();
  • promiseItAllSettled()
const { promiseItAllSettled } = require('@msaaddev/promise-it');
const { exec } = require('child_process');

const { promiseIt, promiseItAll, promiseItAny } = require('./index');
const { exec } = require('child_process');

(async () => {
	const callback = (resolve, reject) => {
		exec(`networkQuality`, error => {
			if (error) {
				handleError(error);
				reject(error);
			}
			resolve('All good. Ran networkQuality.');
		});
	};

	const callback2 = (resolve, reject) => {
		exec(`networkQuality -v`, error => {
			if (error) {
				reject(error);
			}
			resolve('All good. Ran networkQuality -v.');
		});
	};

	try {
		const allSettledPromise = await promiseItAllSettled(
			callback,
			callback2
		);
		console.log(allSettledPromise);
	} catch (err) {
		console.log(err);
	}
})();

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Contributing

Make sure you read the contributing guidelines before opening a PR.

โšก๏ธ Other Projects

I have curated a detailed list of all the open-source projects I have authored. Do take out a moment and take a look.

๐Ÿ”‘ License & Conduct

promise-it's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.