Git Product home page Git Product logo

proposal-await-all's Introduction

About me

I'm a fun, energetic, resourceful programmer with a passion for JavaScript, and particularly metaprogramming and esoteric TypeScript. I've spent the last 5 years working with TypeScript/React and the MERN stack at Anark, though programming has been a passion of mine since childhood.

proposal-await-all's People

Contributors

mrjacobbloom avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

proposal-await-all's Issues

Revisit async do? Or play more with defer keyword?

Revisiting this after a few years, the await.all proposal feels kinda footgunny (everything runs in parallel inside the brackets??) A better solution would probably look more like async do block (maybe with even fewer brackets though?) A few ideas:

Bracketless async do expresisons

Bracketless async do expresisons are a more generally-useful syntax and parts of it already have community support. The likeliest route to success is probably just to support this feature-set on those proposals

async function initialize() {
  await Promise.all([
    async do let foo = (await request('foo.json')).data,
    async do let bar = (await request('bar.json')).data,
    async do let baz = (await request('baz.json')).data,
  ]);
  render(foo, bar, baz);
}

async do let is so verbose omg, it feels like casting a spell. But it's 8 fewer brackets than the equivalent IIAFE AND saves us from having to do a separate declaration step!

There's an open bug in the do-expressions proposal regarding bracketless-do. It looks like it'd cause all sorts of operator precedence confusion: tc39/proposal-do-expressions#1 -- There's been no movement on this since August 2021 but the conversation doesn't look like it's come to a clear conclusion either

Someone in that bug already made note of the scope thing but I don't think they realized the potential power of what they were saying: tc39/proposal-do-expressions#1 (comment)

Async do expressions haven't seen any action since Feb 2021, but there's now a standalone proposal for them

defer Expression

This is a similar feature inspired by the IcedCoffeeScript keyword mentioned in the Prior Art section of the proposal.

Transpilation: defer Expression -> (async () => Expression)() (may require curlies around arrow function body? idk)

async function initialize() {
  let foo, bar, baz;
  await Promise.all([
    defer foo = (await request('foo.json')).data,
    defer bar = (await request('bar.json')).data,
    defer baz = (await request('baz.json')).data,
  ]);
  render(foo, bar, baz);
}

defer Ident = Expression

It would be even more convenient if it somehow also handled declaration in the surrounding scope?

Transpilation A: defer Ident = Expression ->

/* injected before current statement: */ let Ident;
/* inline: */ (async () => { Ident = Expression })()

Transpilation B: defer Ident = Expression -> (do { var Ident; }, (async () => { Ident = Expression })())

async function initialize() {
  await Promise.all([
    defer foo = (await request('foo.json')).data,
    defer bar = (await request('bar.json')).data,
    defer baz = (await request('baz.json')).data,
  ]);
  render(foo, bar, baz);
}

Userland defer

Could use some kind of "registry" that tacks all values onto an awaited map object... which is kind of like defer except it adds to an object instead of the global scope

class PromiseValueCollector<Mapping extends Record<string | number | symbol, any> = Record<string | number | symbol, any>> {
    private anyNamed = false;
    private registered: { promise: Promise<any>, name: keyof Mapping }[] = [];
    public register<K extends keyof Mapping>(promise: Promise<K>, name?: K): void {
        if (name !== undefined) this.anyNamed = true;
        this.registered.push({ promise, name: name || this.registered.length });
    }
    public async all(): Promise<Mapping> {
        if (this.anyNamed) {
            const out = {} as Mapping;
            await Promise.all(this.registered.map((r) => r.promise.then((retVal) => { out[r.name] = retVal })));
            return out;
        } else {
            return Promise.all(this.registered.map((r) => r.promise)) as unknown as Mapping;
        }
    }
}

interface Promise<T> {
    defer: (registry: PromiseValueCollector, name?: string) => void;
}

Promise.prototype.defer = function registerOn(registry: PromiseValueCollector, name?: string) {
    registry.register(this, name);
};


/// Driver
(async () => {
    const getTimeIn = (secs: number) => new Promise(r => { setTimeout(() => r(new Date), secs * 1000); });

    const collector1 = new PromiseValueCollector();
    getTimeIn(0).defer(collector1, 'timeIn0');
    getTimeIn(1).defer(collector1, 'timeIn1');
    getTimeIn(2).defer(collector1, 'timeIn2');
    console.log(await collector1.all());

    const collector2 = new PromiseValueCollector();
    getTimeIn(0).defer(collector2);
    getTimeIn(1).defer(collector2);
    getTimeIn(2).defer(collector2);
    console.log(await collector2.all());
})();

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.