npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-sync

v1.1.7

Published

synchronous promise stub

Downloads

26

Readme

promise-sync

Synchronous promise for making testing experience much easier.

Installation

npm install promise-sync --save-dev

Why?

  • Did you ever need to use setTimeout just to make sure the async logic happened before you test the result?
  • Do your tests run slowly because of long waits for async results?
  • Have you ever tried to make a race condition test of 2 promises?
  • Do you want to control the exact moment the promise reolves or rejects?

Well it all happened to me, so I searched for a library to make all of these work as I want. All the results had a few things missing, some were async, some didn't have the chaining, some threw errors if you didnt subscribe to them.

So I decided to write a new one.

Who?

Well, it was developed by me using TDD and checking the behaviour of a real promise.

Features

  • Support callback subscription: success(), catch(), then(), finally()
  • Chaining callback subscriptions
  • Support state checking: state, isPending(), isRejected(), isFulfilled()
  • Support resolving and rejecting: resolve(data?), reject(reason?)
  • Make it possible to ignore assertion errors inside the success/failure/finally callbacks.
  • Create a resolved/rejected promise with one simple line.
  • Wait for all promises to resolve using PromiseMock.all() method.
  • Wait for one of the promises to resolve using PromiseMock.race() method.
  • Written in Typescript so its type-safe
  • Resolving or rejecting not pending promise will throw error
  • Subscribing to resolved promise will raise proper callbacks
  • Subscribing to rejected promise will raise proper callbacks
  • Synchronous!

Usage examples:

Typescript:
import { PromiseMock, PromiseState } from 'promise-sync';

var promiseMock = new PromiseMock<number>();

var onSuccessCallback = () => console.log('success');
var onFailureCallback = () => console.log('failure');
var finallyCallback = () => console.log('finally');

promiseMock.then(onSuccessCallback, onFailureCallback)
           .success(onSuccessCallback)
           .catch(onFailureCallback)
           .finally(finallyCallback);

var dataToResolve = 123;
promiseMock.resolve(dataToResolve);
// Or:
var errorToReject = 'some error';
promiseMock.reject(errorToReject);

var isPending = promiseMock.isPending();
var isFulfilled = promiseMock.isFulfilled();
var isRejected = promiseMock.isRejected();

var state: PromiseState = promiseMock.state;

var rejectedPromise: PromiseMock<string> = PromiseMock.reject('some error');
var resolvedPromise: PromiseMock<string> = PromiseMock.resolve('some data');

var waitForAll: PromiseMock<any[]> = PromoseMock.all(
  [
    rejectedPromise,
    resolvedPromise,
    'random data that will be converted to resolved promise'
  ]);

var waitForFirst: PromiseMock<any> = PromoseMock.race(
  [
    rejectedPromise,
    resolvedPromise,
    'random data that will be converted to resolved promise'
  ]);
Same example using javascript
var promise_sync = require('promise-sync');
var PromiseMock = promise_sync.PromiseMock;
var PromiseState = promise_sync.PromiseState;

var promiseMock = new PromiseMock();

var onSuccessCallback = function() { console.log('success'); }
var onFailureCallback = function() { console.log('failure'); }
var finallyCallback = function() { onsole.log('finally'); }

promiseMock.then(onSuccessCallback, onFailureCallback)
           .success(onSuccessCallback)
           .catch(onFailureCallback)
           .finally(finallyCallback);

var dataToResolve = 123;
promiseMock.resolve(dataToResolve);

var errorToReject = 'some error';
promiseMock.reject(errorToReject);

var isPending = promiseMock.isPending();
var isFulfilled = promiseMock.isFulfilled();
var isRejected = promiseMock.isRejected();

var state = promiseMock.state;

var rejectedPromise = PromiseMock.reject('some error');
var resolvedPromise = PromiseMock.resolve('some data');

var waitForAll = PromoseMock.all(
  [
    rejectedPromise,
    resolvedPromise,
    'random data that will be converted to resolved promise'
  ]);

var waitForFirst = PromoseMock.race(
  [
    rejectedPromise,
    resolvedPromise,
    'random data that will be converted to resolved promise'
  ]);

Be aware!

The methods: 'then/success/catch/finally' catch exceptions thrown in the callbacks. So if you want to do assertions inside of them you need to tell the PromiseMock to ignore assertion error exceptions, otherwise the tests will pass even though the assertions are failing

If you are using chai for example:

Typescript:
import { AssertionError } from 'chai';
import { PromiseMock } from 'promise-sync';

PromiseMock.setAssertionExceptionTypes([AssertionError]);
Same example using javascript
var chai = require('chai');
var promise_sync = require('promise-sync');
var PromiseMock = promise_sync.PromiseMock;

PromiseMock.setAssertionExceptionTypes([chai.AssertionError]);