Skip to content

Instantly share code, notes, and snippets.

@gasp
Last active July 9, 2018 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gasp/b9c7f1bad82c4ea878b106f44a2bebfc to your computer and use it in GitHub Desktop.
Save gasp/b9c7f1bad82c4ea878b106f44a2bebfc to your computer and use it in GitHub Desktop.
call async function on each array items
const test = require('ava')
// testing the logic
const event = {
Records: [
{
Sns: {
a: 1,
b: 2,
}
},
{
Sns: {
a: 3,
b: 4,
}
},
]
};
const actions = event.Records.map((record) => {
return {
a: record.Sns.a,
b: record.Sns.b,
}
})
const provider = (message) => {
return `p:${message}`
}
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const af = (provider) => async ({ a, b }) => {
await timeout(50);
return { a: provider(a), b: provider(b) }
}
test('await all promise', async t => {
const res = await Promise.all(actions.map(async act => (await af(provider)(act))))
t.deepEqual(res, [{ a: 'p:1', b: 'p:2' }, { a: 'p:3', b: 'p:4' }]);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment