Code Monkey home page Code Monkey logo

process-pending-promises's Introduction

Process Pending Promises 🤠

This is a single file utility which helps in processing all the pending promises.

The idea is, since our code is riddled with Promises and promises do not rely on next ticks, it becomes harder to actually test the side effects caused by them.

The big idea here is to rely on async/await and Promise to resolve itself. This kind of processing allows all the previous promises to execute.

Example:

If we don't flush the promises, this happens:

  it('does not update the value when not flushed', async () => {
    const promise = new Promise((resolve, reject) => {
      resolve('runs');
    });

    let assertValue = 'does not run';

    promise.then((data) => {
      assertValue = data;
    });

    expect(assertValue).toBe('does not run');
  });

The above thing is not ideal since you'd want the promise to be executed and assign the value to assertValue. This is where process-pending-promises comes into play. We can update the above code to something like this which works how we envision it:

  import flush from 'process-pending-promises';

  it('updates the value when flushed', async () => {
    const promise = new Promise((resolve, reject) => {
      resolve('runs');
    });

    let assertValue = 'does not run';

    promise.then((data) => {
      assertValue = data;
    });

    await flush();

    expect(assertValue).toBe('runs');
  });

Also, this library will work with setTimeout's too since sinon's fake timers can fast forward the time.

process-pending-promises's People

Contributors

thisisrajat 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.