This file contains hidden or 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 p = new Promise((resolve, reject) => { | |
if (Math.random() > 0.5) resolve('yay') | |
reject('no') | |
}) | |
p | |
.then(function acao1 (res) { console.log(`${res} da ação 1`); return res; }) | |
.catch(function erro1 (err) { console.error('Primeiro catch'); return 'Error'; }) | |
.then(function acao2 (res) { console.log(`${res} da ação 2`); return res; }) | |
.then(function acao3 (res) { console.log(`${res} da ação 3`); return res; }) |
This file contains hidden or 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 p = new Promise((resolve, reject) => { | |
if (Math.random() > 0.5) resolve('yay') | |
reject('no') | |
}) | |
p | |
.then(function acao1 (res) { console.log(`${res} da ação 1`); return res; }) | |
.then(function acao2 (res) { console.log(`${res} da ação 2`); return res; }) | |
.then(function acao3 (res) { console.log(`${res} da ação 3`); return res; }) | |
.catch(function erro (rej) { console.error(rej) }) |
This file contains hidden or 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 p = new Promise((resolve, reject) => { | |
if (Math.random() > 0.5) resolve('yay') | |
reject('no') | |
}) | |
p.then(console.log).catch(console.error) |
This file contains hidden or 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 p = new Promise((resolve, reject) => { | |
setTimeout(() => resolve('yay'), 5000) | |
}) | |
p | |
.then((res) => {}) | |
.catch((rej) => {}) |
This file contains hidden or 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 p = new Promise((resolve, reject) => { | |
setTimeout(() => resolve('yay'), 5000) | |
}) | |
p.then((res) => {}) | |
p.catch((rej) => {}) |
This file contains hidden or 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 p = new Promise((resolve, reject) => { | |
setTimeout(() => resolve('yay'), 5000) | |
}) | |
// Ambos os callbacks estão ligados a p | |
p.then((res) => {}, (rej) => {}) | |
// Isto aqui é a mesma coisa | |
new Promise((resolve, reject) => {}) | |
.then((res) => {}, (rej) => {}) |
This file contains hidden or 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
// Criando uma promise | |
const p = new Promise((resolve, reject) => { | |
try { | |
resolve(funcaoX()) | |
} catch (e) { | |
reject(e) | |
} | |
}) | |
// Executando uma promise |
This file contains hidden or 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
fetch('api') | |
.on('error', err => { /* faça algo */ } | |
.on('data', dados => { /* faça algo */ } |
This file contains hidden or 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 fs = require('fs') | |
const { promisify } = require('util') | |
const readFilePromise = promisify(fs.readFile) | |
const writeFilePromise = promisify(fs.writeFile) | |
function outraFuncaoAssincrona (parametro) { | |
return new Promise((resolve, reject) => { | |
resolve(parametro.split(',')) | |
}) | |
} |
This file contains hidden or 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 fs = require('fs') | |
function callbackRead(err, dados) { | |
if (err) throw new Error(err) | |
fs.writeFile('./outroarquivo.txt', dados, callbackWrite) | |
} | |
function callbackWrite (err) { | |
if (err) throw new Error(err) | |
outraFuncaoAssincrona(callbackAsync1) |