Created
October 3, 2016 14:01
-
-
Save mauriciofs/76c4e42d2980bb0979cb7d62152247ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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