'Promise.all with mongoose

I'm trying to await multiple mongoose promises.

The first version of the code :

const func = async (param) => {
    let a = undefined, b = undefined;

    a = mongoose.model('a').findOne({ name: '1' }).exec()
    if (param === 1) b = mongoose.model('b').findOne({ name: '1' }).exec()

    await Promise.all([a, b]);
    console.log(a, b)
}

The second version of the code :

const func = async (param) => {
    const requests = await Promise.all([
        mongoose.model('a').findOne({ name: '1' }),
        param === 1 ? mongoose.model('b').findOne({ name: '1' }) : undefined
    ])

    const a = requests[0], b = requests[1];

    console.log(a, b)
}

Expected output of the first version would be : "<ModelA> <ModelB>"

But I have : "Promise {<ModelA>} Promise {<ModelB>}"

The second version is working as expected

Any idea of what I'm doing wrong in the first version ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source