Skip to content

Smarter type checking for Promise.all #24987

Closed
@tmkn

Description

@tmkn

TypeScript Version: 3.0.0-dev.201xxxxx

Search Terms:
promise.all

Code
First off, idk if this can be considered a bug or an improvement.
The code is legit but it's probably not what you intended to write or what you would expect to look at when you have to debug JS code.

Basically the code wanted to await a bunch of Promises via Promise.all but the Promises array was accidentally nested:

let delay = (delay: number) => {
    return new Promise(resolve => {
        window.setTimeout(() => {
            console.log("done: ", delay)
            resolve();
        }, delay);
    })
}

let delays = [1000, 2000];

(async () => {
    console.log("start");
    await Promise.all([delays.map(ms => delay(ms))]);//accidentally nested promises array
    console.log("end");
})();

Expected behavior:
"start"
"done: 1000"
"done: 2000"
"end"

Actual behavior:
"start"
"end"
"done: 1000"
"done: 2000"
which is of course correct but probably not what you intended to write.

It would be nice if TypeScript could pick this up and report it.

Playground Link:
http://www.typescriptlang.org/play/index.html#src=let%20delay%20%3D%20(delay%3A%20number)%20%3D%3E%20%7B%0D%0A%20%20%20%20return%20new%20Promise(resolve%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20window.setTimeout(()%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(%22done%3A%20%22%2C%20delay)%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20resolve()%3B%0D%0A%20%20%20%20%20%20%20%20%7D%2C%20delay)%3B%0D%0A%20%20%20%20%7D)%0D%0A%7D%0D%0A%0D%0Alet%20delays%20%3D%20%5B1000%2C%202000%5D%3B%0D%0A%0D%0A(async%20()%20%3D%3E%20%7B%0D%0A%20%20%20%20console.log(%22start%22)%3B%0D%0A%20%20%20%20await%20Promise.all(%5Bdelays.map(ms%20%3D%3E%20delay(ms))%5D)%3B%2F%2Faccidentally%20nested%20promises%20array%0D%0A%20%20%20%20console.log(%22end%22)%3B%0D%0A%7D)()%3B

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions