SNS.publish requires a callback while using promise() and sends 2 events for each invocation

Jose Cuervo
1 min readApr 1, 2021

I recently ran into this issue which took me longer than I would have liked to figure out.

I was using the promise() version of the SNS.publish method. Typescript was complaining that it required 2 arguments(params and callback) but I was only supplying 1 argument(params). So I added the callback to make typescript happy.

const sns = new SNS();
await sns
.publish(
{
Message: 'Message',
TopicArn: <Arn>,
},
(error) => {
if (error) {
console.log('error happened');
console.error(error);
}
},
)
.promise();

This results in the event being published twice.

According to this post on stackoverflow, sns is publishing the event once for the callback, and again for the promise.

The key to get around this and make typescript happy is specifying the API version in the SNS constructor

const sns = new SNS({ apiVersion: '2010-03-31' });
await sns
.publish(
{
Message: 'Message',
TopicArn: <Arn>,
},
)
.promise();

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Jose Cuervo
Jose Cuervo

Written by Jose Cuervo

0 Followers

I write about pitfalls I fall into because developer documentation is bad, and how to dig yourself out.

No responses yet

Write a response