Sonali
Sonali

Reputation: 525

Lambda function returning a promise from a function not working

I am working on lambda function using node js. I calling a function which return a promise on then I am calling a promiseAll. My first function gets a row form DB based on ID, if row is not present I want to return error response and if present it should call promiseAll.

Following is my code:

return getDetailsOfRow(id).then(function() {
    return Promise.all([
        getDetailsOfA(query1), 
        getDetailsOfB(query2), 
        getDetailsOfC(tripLocationsQuery)
    ]).then(function(values) {
        return combineResults();
    })
});

function getDetailsOfRow(trip) {
    return new Promise((resolve, reject) => {
        con.query(query, id, (err, results, fields) => {
            if (err) {
                reject(err);
            }
            if (results.length < 1) {
                createErrorResponse();
            } else {
                //get the set column values to use for other function   
            }
        });
    });
}

function createErrorResponse() {
    return new Promise((resolve, reject) => {
        var response = {
            "isBase64Encoded": false,
            "statusCode": 404,
            "headers": {
                "Content-Type": "text/html"
            },
            "body": "Invalid Key"
        };
        resolve(response);
    });
}

promise inside createErrorResponse method is not getting called for lambda function. I am not getting any response from a lambda function. I didn't find any solution. Is it because I will br returning a promise inside a promise if call createErrorResponse() from getDetailsOfRow() ?

Upvotes: 1

Views: 1411

Answers (2)

Ashish Modi
Ashish Modi

Reputation: 7770

I see two problems here

  • You are wrapping the code for createErrorResponse in promise which is not required.
  • You are not resolving in getDetailsOfRow method so there is no way to fulfill the promise.

The code should look like this

return getDetailsOfRow(id).then(function() {
  return Promise.all([
    getDetailsOfA(query1),
    getDetailsOfB(query2),
    getDetailsOfC(tripLocationsQuery)
  ]).then(function(values) {
    return combineResults();
  }).catch( error => {
     return error;
  });
});

function getDetailsOfRow(trip) {
  return new Promise((resolve, reject) => {
    con.query(query, id, (err, results, fields) => {
      if (err) {
        reject(err);
      }
      if (results.length < 1) {
        reject(createErrorResponse());
      } else {
        resolve(results); // it should be resolved.
      }
    });
  });
}

function createErrorResponse() {
  return {
    isBase64Encoded: false,
    statusCode: 404,
    headers: {
      "Content-Type": "text/html"
    },
    body: "Invalid Key"
  };
}

Upvotes: 0

Sohail Ashraf
Sohail Ashraf

Reputation: 10569

You don't need to wrap createErrorResponse function in a promise.

Add the createErrorResponse() in resolve or reject and add catch block in getDetailsOfRow to catch the error.

Try this


return getDetailsOfRow(id).then(function() {
  return Promise.all([
    getDetailsOfA(query1),
    getDetailsOfB(query2),
    getDetailsOfC(tripLocationsQuery)
  ]).then(function(values) {
    return combineResults();
  }).catch( error => {
     return error;
  });
});

function getDetailsOfRow(trip) {
  return new Promise((resolve, reject) => {
    con.query(query, id, (err, results, fields) => {
      if (err) {
        reject(err);
      }
      if (results.length < 1) {
        reject(createErrorResponse());
      } else {
        //get the set column values to use for other function
      }
    });
  });
}

function createErrorResponse() {
  return {
    isBase64Encoded: false,
    statusCode: 404,
    headers: {
      "Content-Type": "text/html"
    },
    body: "Invalid Key"
  };
}

Upvotes: 1

Related Questions