News for hackers. Stuff that matters.
Re:Modern Node.js Patterns by mythz on Sunday August 03, 2025 @11:30PM (#44781898)
why not?

    const data = await (await fetch(url)).json()
Re:Modern Node.js Patterns by mcv on Monday August 04, 2025 @02:31AM (#44782672)
That's very concise. Still, the double await remains weird. Why is that necessary?
Re:Modern Node.js Patterns by sethaurus on Monday August 04, 2025 @05:23AM (#44783539)
The first `await` is waiting for the response-headers to arrive, so you know the status code and can decide what to do next. The second `await` is waiting for the full body to arrive (and get parsed as JSON).

It's designed that way to support doing things other than buffering the whole body; you might choose to stream it, close the connection early etc. But it comes at the cost of awkward double-awaiting for the common case (always load the whole body and then decide what happens next).

Re:Modern Node.js Patterns by moritzruth on Monday August 04, 2025 @05:25AM (#44783549)
IMU because you don't necessarily want the response body. The first promise resolves after the headers are received, the .json() promise resolves only after the full body is received (and JSON.parse'd, but that's sync anyway).
Re:Modern Node.js Patterns by jech on Monday August 04, 2025 @07:09AM (#44784226)
So you can say:

    let r = await fetch(...);
    if(!r.ok) ...
    let len = response.headers.get("Content-Length");
    if(!len || new Number(len) > 1000 * 1000)
        throw new Error("Eek!");
Re:Modern Node.js Patterns by tracker1 on Monday August 04, 2025 @12:40PM (#44788231)
It isn't, the following works fine...

    var data = await fetch(url).then(r => r.json());
Understanding Promises/A (thenables) and async/await can sometimes be difficult or confusing, especially when mixing the two like above.
Re:Modern Node.js Patterns by mcv on Thursday August 07, 2025 @12:35PM (#44826702)
Same thing. Maybe this doesn't make the double promise quite as visible, but it's still a double promise. You could probably replace the other await with a .then() too.