Closed dbeja closed 6 years ago
Ok, figured it out. This is a callback so removeTokens wouldn't be waiting for the function to finish. I just added the rest of the code needed after getting the contact inside removeTokens callback and it's working fine!
You would need to wrap the callback-based API with a Promise
to make it compatible with async/await. Something like this:
import {RateLimiter} from 'limiter';
main()
async function main() {
const dynamoDBLimiter = new RateLimiter(10, 'second');
console.log('before removeTokens');
for(let contact_i = 0; contact_i < company.contacts.length; contact_i++) {
await removeTokens(1, dynamoDBLimiter)
console.log('adding contact');
const newContact = await addContact(contact_i);
console.log('new contact added');
}
console.log('end of removeTokens');
}
function removeTokens(count, limiter) {
return new Promise((resolve, reject) => {
limiter.removeTokens(count, (err, remainingRequests) => {
if (err) return reject(err)
resolve(remainingRequests)
})
})
}
Hi,
I have something like this:
But the logs I get are like this:
Am I doing something wrong? Thanks!