简体   繁体   中英

Using a callback inside of AWS Lambda handler

I've been struggling with trying to output the success/error of my promise chain.

I am chaining promises together like this

exports.handler = function(event, context, callback) {
    Q.allSettled(init()).then(function (result) {

    requestValid(event.queryStringParameters)
        .then(isDuplicate)
        .then(process)
        .then(insertData)
        .then(displayResponse)
        .catch(function (error) {
            // Handle any error from all above steps
            console.error(
                'Error: ' + error
            );
            callback({
                statusCode: 500,
                body: JSON.stringify({
                    message: error
                }, null)
            });
        })
        .done(function () {
            console.log(
                'Script Finished'
            );
            callback(null, {
                statusCode: 200,
                body: JSON.stringify({
                    message: 'Done'
                })
            });
        });
    });
};

I am calling Q.defer.reject(error_message); on fail and Q.defer.resolve(success_message) on success inside of the promises. If any of these promises fail, the error is being caught in .catch(function (error) { .

That's all fine but how do I return this data to the handler's callback?

Example of what I want to do, but don't how/where to put it because of the promises...

exports.handler = function (event, context, callback) {
    let response = {
        statusCode: 200,
        body: JSON.stringify('some success or error')
    };

// Return all of this back to the caller (aws lambda)
    callback(null, response);
};

Thank you in advance..

Chain your promises all the way you usually do it . Return the result on the last step of the chain. catch errors at the end, and return the error if any:

exports.handler = function(event, context, callback) {

   RequestIsValid(event)
    .then(isDuplicate)
    .then(process)
    .then(insertData)
    .then(getResponse)
    .then(function(result) {
        //result would be the returning value of getResponse.
        callback(null, {
            statusCode: 200,
            body: JSON.stringify({
                message: 'Done',
                response: result
            })
        });
    })
    .catch(function (error) {
        // this will show on CloudWatch
        console.error(
            'Error: ' + error
        );
        // this will be the response for the client
        callback({
            statusCode: 500,
            body: JSON.stringify({
                message: error
            }, null)
        });
    })

// end of the function
}

That's assuming that isDuplicate , process , insertData and getResposne return a promise and the result of each can be chained to the next one.

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