Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active October 4, 2017 23:26
Show Gist options
  • Save Woodsphreaker/f35e62fee0e4f20f0a440320bb3fd1ea to your computer and use it in GitHub Desktop.
Save Woodsphreaker/f35e62fee0e4f20f0a440320bb3fd1ea to your computer and use it in GitHub Desktop.
services async - ( promise )
const obj = [
{
nome: 'User1',
cpf: '000.000.000-00',
time: 1000,
result: 0,
},
{
nome: 'User2',
cpf: '111.000.000-00',
time: 1200,
result: 0,
},
{
nome: 'User3',
cpf: '222.000.000-00',
time: 1400,
result: 0,
},
{
nome: 'User4',
cpf: '333.000.000-00',
time: 1600,
result: 0,
},
{
nome: 'User5',
cpf: '444.000.000-00',
time: 1800,
result: 0,
},
]
const timeDelay = ( nome, time = 1000 ) =>
new Promise(resolve => setTimeout(() => resolve( `Process ${nome} in ${time} ms` ), time) )
const callServices = data =>
Promise.all(data.map( el => timeDelay( el.nome, el.time ) ) )
const processData = data => ( el, i ) => {
return {
nome: el.nome,
cpf: el.cpf,
result: data[i],
}
}
const asyncMethod1 = data =>
new Promise(resolve => callServices( data ).then(obj => resolve( data.map( processData( obj ) ) ) ) )
asyncMethod1( obj ).then(result => console.log( result ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment