Git Product home page Git Product logo

promise-all-reject-late's Introduction

promise-all-reject-late

Like Promise.all, but save rejections until all promises are resolved.

This is handy when you want to do a bunch of things in parallel, and rollback on failure, without clobbering or conflicting with those parallel actions that may be in flight. For example, creating a bunch of files, and deleting any if they don't all succeed.

Example:

const lateReject = require('promise-all-reject-late')

const { promisify } = require('util')
const fs = require('fs')
const writeFile = promisify(fs.writeFile)

const createFilesOrRollback = (files) => {
  return lateReject(files.map(file => writeFile(file, 'some data')))
    .catch(er => {
      // try to clean up, then fail with the initial error
      // we know that all write attempts are finished at this point
      return lateReject(files.map(file => rimraf(file)))
        .catch(er => {
          console.error('failed to clean up, youre on your own i guess', er)
        })
        .then(() => {
          // fail with the original error
          throw er
        })
    })
}

API

  • lateReject([array, of, promises]) - Resolve all the promises, returning a promise that rejects with the first error, or resolves with the array of results, but only after all promises are settled.

promise-all-reject-late's People

Contributors

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