15

I have a DynamoDB Put request wrapped into an async function.

 async function putter(param1, param2) {

        const paramsPut = {
            TableName: MyTableName,
            Item: {
                "hashKey": param1,
                "sortKey": param2,
            }
        };

        dynamodb.put(paramsPut, function(err, data) {

            if (err) {
                console.log("Failure")
                console.log(data)
                return data
            }
            else {
                console.log("Success")
                console.log(data)
                return data
            }
        });
    };

The return for the async funtion is placed in the response function - this the should provide back a promise upon put operation was performed (either sucessfully or not sucessfully).

I then invoke this async put function from another async function:

    var param1 = "50";
    var param2 = "60";


    async function main() {

        await putter(param1 , param2)
        console.log("Feedback received")

    }

When I invoke this aysnc main function I would expect it to provide the Success statement from the put function prior to writing "Feedback received" as it should await the put function response.

However my console logs the "Feedback received" prior to logging the "Success" statement in the put async function which I was awaiting.

What am I missing here? Thanks for your support!

2
  • Can please share complete code after modification(Working Code) Commented Jan 7, 2021 at 10:46
  • Please share code for get data from dynamodb also. Commented Jan 7, 2021 at 10:53

1 Answer 1

50

Try to change your code like follows:

try {
    const data = await dynamodb.put(paramsPut).promise()
    console.log("Success")
    console.log(data)
    return data
} catch (err) {
    console.log("Failure", err.message)
    // there is no data here, you can return undefined or similar
}

Almost every function from AWS SDK has the promise() variant to return the result as a Promise. Then you can just await the Promise. Don't mix callbacks with promises (async/await) - it makes the code hard to read, it's better to stick with one technique everywhere.

Sign up to request clarification or add additional context in comments.

6 Comments

Dear Tomas, that worked perfectly! Thank you so much. Do you have any idea, why the previous code did not work? I was expecting the "return" in the async function to return a promise when placed into the function called after put-completion...
Your problem was that calling return in a callback function has no effect at all, because the callback is in fact an anonymous function and no one is interested in its return value, so it will be ignored and the wrapping function ends up always returning nothing. Happy to help, you can accept the answer then.
Nice one it helped. BTW, what is the difference put and putItem I only saw putItem?
@Xin Those are different APIs: DynamoDB::putItem vs DynamoDB.DocumentClient::put.
@ttulka Thank you. DOcumentClient is more well interface frendly, easier to use. thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.