4

Started reading during meetings. The description of `elegance` vs `marketable` in "Hacking: The art of exploitation" hits home. Probably also why I hate the client work I need to do so much right now as well, there is nothing elegant about react

Thanks for the recommendation @retoor

Comments
  • 1
    WHAT?

    Bitch React is super elegant. React wears gucci for its proctologist appointment.
  • 2
    Just look. My collegue had a bunch of api calls to group by ten.

    This guy did it like this:

    const results: { hash: string, status: boolean }[] = [];

    try {

    for (let i = 0; i < tasks.length; i += BATCH_SIZE) {

    const batch = tasks.slice(i, i + BATCH_SIZE);

    const batchPromises = batch.map((t) => t.promiseFn());

    const batchResults = await Promise.allSettled(batchPromises);

    batchResults.forEach((res, index) => {

    if (res.status === 'fulfilled') {

    results.push({

    What a fucking psychopath.
  • 2
    the correct answer being of course something like:

    const sliceByBatches = (tasks: Task[]) => {

    return range(0, Math.ceil(tasks.length / BATCH_SIZE)).map(batchNumber =>

    tasks.slice(batchNumber * BATCH_SIZE, (batchNumber + 1) * BATCH_SIZE)

    );

    };

    const results = await sliceByBatches(batches).reduce<Promise<Result[]>>(async (results, batchTasks) => {

    const batchResults = await Promise.allSettled(batchTasks.map(t => t.promiseFn()));

    const processed = batchResults.map((res, index) => ({

    XXXX

    }));

    return [...( await results ), ...processed];

    }, Promise.resolve([]));
  • 1
    @antigermgerm While I agree with your code complaint, you're not actually processing batches since the reduce doesn't wait between iteration for it to complete you probably want something more like this (chunk and sliceByBatches being pretty much the same thing):

    let results = []

    for (const batch of chunk(batches, BATCH_SIZE)) {

    const res = await Promise.allSettled(batch.map(t => t.promiseFn()));

    results = [...results, ...res.map((r, i) => ({ ... }))];

    }
  • 1
    @antigermgerm although this would be the dream if the array funcs could deal with async as async instead of promises and opt to be sequential

    const results = await Promise.all(chunk(batches, BATCH_SIZE).flatMap(async t => {

    const res = await Promise.allSettled(t.map(t => t.promiseFn()));

    return res.map((r, i) => ({ ... }));

    }));
  • 2
    @antigermgerm Gucci is for people who want to look elegant but aren't ;P

    You're welcome to show me good react code, but I disagree with it on a fundamental level
  • 2
    Good book. I have it in *.pdf, though.
Add Comment