Giter Site home page Giter Site logo

p-break's Introduction

p-break

Break out of a promise chain

See "How do I break out of a promise chain?" for a better way.

Install

$ npm install p-break

Usage

Here the onlyRunConditional promises are skipped if conditional is falsy:

import pBreak from 'p-break';

alwaysRun1()
	.then(() => alwaysRun2())
	.then(conditional => conditional || pBreak('πŸ¦„'))
	.then(() => onlyRunConditional1())
	.then(() => onlyRunConditional2())
	.then(() => onlyRunConditional3())
	.then(() => onlyRunConditional4())
	.catch(pBreak.end)
	.then(console.log);
	//=> 'πŸ¦„'

API

pBreak([value])

Starts the break. Any .then()'s between here and pBreak.end() are skipped.

value

Value to pass down the chain after pBreak.end().

pBreak.end

Ends the break. Make sure not to have any other .catch() handlers between pBreak() and here.

Related

p-break's People

Contributors

richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

p-break's Issues

Labels

FWIW, it smells a bit like goto:
GOTO

Hence, introducing labels might be the next step:

alwaysRun1()
    .then(() => alwaysRun2())
    .then(conditional1 => conditional1 || pBreak('πŸ¦„', 'label1'))
    .then(conditional2 => conditional2 || pBreak('πŸ¦„', 'label2'))
    .then(() => onlyRunConditional1())
    .then(() => onlyRunConditional2())
    .catch(pBreak.end('label1'))
    .then(() => onlyRunConditional3())
    .then(() => onlyRunConditional4())
    .catch(pBreak.end('label2'))
    .then(console.log);

– Totally spaghetti, in my opinion πŸ˜‰

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.