简体   繁体   中英

AWS node.js wait for function

so I'm pretty new to node.js and javascript in general. I've been trying to write an alexa smarthome skill which turns on my light. I host my Node.js function in the amazon aws cloud using lambda. Alexa calls this function passing an event and context object and a callback I should use to set the result. However, if the "main" function finishes its execution before the callback is set it doesn't work and the result is "null". So in order to make it work I think I need wait somehow for the webrequest to complete before ending the "main" function. I attached my current code (truncated) so you can hopefully figure out what I mean and suggest me a solution for this. Thank you!

EDIT FORGOT CODE ^^

process.env["PATH"] = process.env["PATH"] + ":" + process.env["LAMBDA_TASK_ROOT"];

const uuidV4 = require('uuid/v4');
var https = require('https');

var defaultRGB = {r: 100, g: 100, b: 100};

var handlers = {
    "DiscoverAppliancesRequest": discoverAppliancesRequestHandler,
    "TurnOnRequest": turnOnRequestHandler,
    "TurnOffRequest": turnOffRequestHandler
};


function turnOnRequestHandler(event, context, callback) {
    var header = buildHeader("TurnOnConfirmation", "Alexa.ConnectedHome.Control");
    var body = {};
    turnOnAllLights(function () {
        callback(null, {header: header, payload: body});
    });
}
function turnOnAllLights(callback) {
    var options = {
        host: "home.XXXX.de",
        port: 9443,
        path: "/XXX/update/V1",
        method: "PUT",
        headers: {
            "Content-Type": "application/json"
        }
    };

    var request = https.request(options, function (res) {
        console.log(res.statusCode);
        res.on("data", function (chunk) {
            console.log(chunk);
            callback();
        }).on("error", function (error) {
            console.error(error);
            callback();
        })
    });
    request.on("error", function (error) {
        console.error(error);
    });
    request.write(JSON.stringify([defaultRGB.r, defaultRGB.g, defaultRGB.b]));
    request.end();
}

function buildHeader(name, namespace) {
    return {
        messageId: uuidV4(),
        name: name,
        namespace: namespace,
        payloadVersion: 2
    };
}

exports.handler = function (event, context, callback) {
    var name = event.header.name;
    handlers[name](event, context, callback);
};

The function completing before your code is usually a sign you need to increase the timeout setting of your Lambda function under the advance settings in your configuration.

Make sure you set your timeout more then the length you would expect your function to load, run code and web request take. I think the default is 10 sec which may not be enough for you.

Hope it helps

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