Git Product home page Git Product logo

iced-defer's Introduction

iced-defer

A failed attempt at a utility for JavaScript Promises, inspired by IcedCoffeeScript's "defer" keyword.

The goal was to sidestep the "parallel arrays" required by Promise.all:

async function initialize() {
  const [
    { data: foo }, // rename response.data => foo using destructuring
    { data: bar },
    { data: baz },
  ] = await Promise.all([
    request('foo.json'),
    request('bar.json'),
    request('baz.json'),
  ]);
  render(foo, bar, baz);
}

The approach I was playing with was to use an object that collects promises and returns an object with named properties:

import { Defer } from 'iced-defer';
async function initialize() {
  const defer = Defer<{ data: Date }>();
  request('foo.json').then(defer('foo'));
  request('bar.json').then(defer('bar'));
  request('baz.json').then(defer('baz'));
  const { foo, bar, baz } = await defer.all();
  render(foo.data, bar.data, baz.data);
}

However, this doesn't work when promises reject, because .then() callbacks only run when the promise resolves. Some possible alternatives:

  • Add a new method to Promise.prototype. Ex. somePromise.register(defer('someKey'))
  • defer() could return a [resolveCallback, rejectCallback] tuple. Ex. somePromise.then(...defer('someKey'))
  • Only provide defer.register(). Ex. defer.register(somePromise, 'someKey'). This works but at that point the library is no easier to use than a vanilla array :/
  • Also attach to promise.catch(). Ex. somePromise.then(defer('someKey')).catch(defer('someKey')). This is clunky though
  • Not viable: Attach to promise.finally() instead. This doesn't work because the result/error aren't passed to .finally callbacks

iced-defer's People

Contributors

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