reles
Front-end (6+ EXP)async-flow
EvolutionPorrr quê?
Atender demandas
{facilitar a leitura do código}
backs
A base
tomia
getSomething(somethingId, (err, data) => {
if (err) {
// :(
return
}
// tudo certo :)
})
getSomething(somethingId, success, failure)
back
Hell (Ryu)
getUserInfo(userId, (err, userInfo) => {
if (err) {
return
}
getFoo(userInfo.attributeId, (err, foo) => {
// ...
getBar(foo, (err, bar) => {
// ...
getAlfredinho(bar, (err, alfredinho) => {
// ...
giveMySoulBack(alfredinho, (err, soul) => {
// ...
pursuitFreedom(soul)
})
})
})
})
})
mises
function pegarPoteDeSorvete(pote) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (pote.conteudo instanceof Sorvete) {
resolve(pote.conteudo)
return
}
reject(pote.conteudo)
}, 5000)
})
}
pegarPoteDeSorvete(poteDaGeladeira)
.then(sorvete => console.log('Hoje Tem :)'))
.catch(feijao => console.log('Hoje Tem Janta :('))
function getUserAddress(userId, cb) {
getUserInfo(userId, (userInfoError, userInfo) => {
if (userInfoError) {
cb(userInfoError)
return
}
cep(userInfo.cep, (cepError, address) => {
if (cepError) {
cb(cepError)
return
}
cb(null, address)
})
})
}
function getUserAddress(userId) {
return getUserInfo(userId)
.then(userInfo => cep(userInfo.cep))
.catch(handleError)
}
Querem escrever código async
de uma maneira sync
etivo
function getUserAddress(userId) {
const userInfo = getUserInfo(userId)
const userAddress = cep(userInfo.cep)
return userAddress
}
rators
por Generator"
Pausável ;)
Tenho múúúltiplos valores :*
Retorno um Iterator
tomia
function *aleatorio() {
yield 'Eu';
yield 'gosto de';
return 'batata.';
}
const iterator = aleatorio(); // {}
iterator.next() // { value: 'Eu', done: false }
iterator.next() // { value: 'gosto de', done: false }
iterator.next() // { value: 'batata.', done: true }
function *fib() {
let current = 0
let next = 1
yield current
while (true) {
[current, next] = [next, current + next]
yield current
}
}
yield
generator.next().value
throw
try { generator.next() } catch(err) { ... }
done
if (generator.next().done) { }
generator.next(10)
data
generator.throw('não tem biscoito')
error
generator.returns(10)
return
async
com cara de sync
function *getUserAddress(userId) {
const userInfo = yield getUserInfo(userId)
const address = yield cep(userInfo.cep)
return address
}
spawn(getUserAddress('zulu-zulu-1')).then().catch()
async
iteration
const generator = getUserAddress('zulu-zulu-1')
let result = generator.next()
result.then(/* ... */)
{ cep: '05361-000' }
result = generator.next({ cep: '05361-000' })
result.then(/* ... */)
{ cep: '05361000', state: 'SP', /* ... */ }
result = { cep: '05361000', state: 'SP', /* ... */ }
const userInfo = yield getUserInfo('zulu-zulu-1')
const userInfo = { cep: '05361-000' }
const address = yield cep(userInfo.cep)
const address = { cep: '05361000', state: 'SP', /* ... */ }
async
iteration
function spawn(generator) {
return new Promise((resolve, reject) => {
const onResult = lastPromiseResult => {
const { value, done } = generator.next(lastPromiseResult)
if (!done) {
value.then(onResult, reject)
return
}
resolve(value)
}
onResult()
})
}
function *getUserAddress(userId) {
const userInfo = yield getUserInfo(userId)
const address = yield cep(userInfo.cep)
return address
}
spawn(getUserAddress('zulu-zulu-1'))
ync / Awa
itNo babel mais perto de você
urity Stag
es
Stage 0
Strawman
Stage 1
Proposal
Stage 2
Draft
Stage 3
Candidate
Stage 4
Finished
tomia
async function getUserAddress(userId) {
const userInfo = await getUserInfo(userId)
const address = await cep(userInfo.cep)
return address
}
{Syntax Sugar}
{ async function *() } { for await (const x of iterable) } { Observables (RxJS) }