简体   繁体   中英

AWS lambda async db (Firestore) query, why returns Promise{<pending>}

var db = admin.firestore();

exports.handler = async (event) => {
    let res = await db.collection(`apps/${event.app_id}/whitelist`).doc(event.ip).set({
        ip: event.ip
    }).then(() => {
        return {
            statusCode: 200,
            body: {
                message: 'success'
            }
        };
    }).catch(() => {
        return {
            statusCode: 400,
            body: {
                message: 'failed'
            }
        };
    });
    return res;
};

This code will result in timeout on Lambda, and console.log(exports.handler(event)) will print Promise{<pending>} .
My questions are
1. What does Lambda expect for this async function; what should it return?
2. Promise{<pending>} means unresolved promise, but in the code, res will finally be a value/object, then why it still says unresolved.
3. What are some nice references we can lookup?

Thanks.

pic

Guys... just allocate more memory for your lambda.

I tried the above code on local and it finished in 0.05s, and it reached 3s time limit on lambda, so I think it should be my promise's error. But in fact, the promise is correct, and it should be a pending promise since the outside function has never been resolved. It is slow since it has too less memory (128mb). Give it more memory and the error goes away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM