Last active
July 9, 2018 09:46
-
-
Save gasp/b9c7f1bad82c4ea878b106f44a2bebfc to your computer and use it in GitHub Desktop.
call async function on each array items
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
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