Skip to content

Instantly share code, notes, and snippets.

View mircoc's full-sized avatar

Mirco Cipriani mircoc

  • Italy
  • 00:14 (UTC +01:00)
View GitHub Profile
// script to test build of a create react app project with an /app_path prefix
// and an api backend served on /api, run it with node
const express = require('express');
const path = require('path');
const app = express();
const proxy = require('http-proxy-middleware');
app.use('/app_path', express.static(path.join(__dirname, 'build'), {}));
@mircoc
mircoc / firstThatCompleteSuccessfullyES6.js
Last active December 13, 2017 14:17
getFirstPromiseThatCompleteSuccessfullyWithinTimeoutES6: Race two or more promises returning the one that finish first and waiting a maximum amount of time. Raw
const firstThatCompleteSuccessfullyES6 = (options) => {
// return the first promise that resolve
const oneSuccess = (promises) => Promise.all(promises.map(p => {
// If a request fails, count that as a resolution so it will keep
// waiting for other possible successes. If a request succeeds,
// treat it as a rejection so Promise.all immediately bails out.
return p.then(
(val) => { return Promise.reject(val); },
(err) => { return Promise.resolve(err); }
@mircoc
mircoc / firstThatCompleteSuccessfully.js
Last active December 13, 2017 11:14
getFirstPromiseThatCompleteSuccessfullyWithinTimeout: Race two or more promises returning the one that finish first and waiting a maximum amount of time.
function firstThatCompleteSuccessfully(options) {
// return the first promise that resolve
function oneSuccess(promises){
return Promise.all(promises.map(
function(p) {
// If a request fails, count that as a resolution so it will keep
// waiting for other possible successes. If a request succeeds,
// treat it as a rejection so Promise.all immediately bails out.
return p.then(