Skip to content

Instantly share code, notes, and snippets.

@mauriciofs
Created October 3, 2016 14:01
Show Gist options
  • Save mauriciofs/76c4e42d2980bb0979cb7d62152247ff to your computer and use it in GitHub Desktop.
Save mauriciofs/76c4e42d2980bb0979cb7d62152247ff to your computer and use it in GitHub Desktop.
receive(){
//Arrow function to not change this context
return new Promise((resolve, reject) => {
let params = {
QueueUrl: this.url,
WaitTimeSeconds: this.waitTimeSeconds,
MaxNumberOfMessages: this.maxNumberOfMessages,
MessageAttributeNames: [
'reprocess'
]
};
console.log(`(${this.url}) GETTING MESSAGES...`);
this.SQS.receiveMessage(params, (err, data) => {
if(err) return reject(err);
console.log('Message', util.inspect(data, false, null));
if (data !== null && data.Messages && data.Messages.length > 0) {
console.log('RESOLVING...'); //Chega aqui depois de um tempo mas não resolve no then
return resolve(data);
}
//Try again
this.receive();
});
});
}
Uso da function:
Sqs.receive().then((data) => {
console.log('SUCCESS');
}).catch((err) => {
console.error(err);
throw new Error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment