Promise
Fulfilled
const condition = true;
const promise = new Promise((resolve, reject) => {
if (condition)
resolve('Valid promise');
else
reject('Invalid promise');
});
promise.then(r => console.log(r));
//=> Valid promise
const condition = true;
const promise = new Promise((resolve, reject) => {
if (condition)
resolve('Valid promise');
else
reject('Invalid promise');
});
promise.then(r => console.log(r));
//=> Valid promise
Rejected
const condition = false;
const promise = new Promise((resolve, reject) => {
if (condition)
resolve('Valid promise');
else
reject('Invalid promise');
});
promise.then(r => console.log(r)).catch(r => console.log(r));
//=> Invalid promise
const condition = false;
const promise = new Promise((resolve, reject) => {
if (condition)
resolve('Valid promise');
else
reject('Invalid promise');
});
promise.then(r => console.log(r)).catch(r => console.log(r));
//=> Invalid promise
Pending
function sum(a, b) {
return a + b;
}
function sumPromise(a, b) {
const promise = new Promise((resolve, reject) => {
if (isNaN(a) || isNaN(b))
reject('Invalid numbers');
else
setTimeout(() => resolve(a + b), 200);
});
return promise
}
console.log(sum(1, 1));
console.log(sumPromise(2, 2));
sumPromise(2, 2)
.then((res) => console.log(res))
.catch((err) => console.log(err));
console.log(sum(3, 3));
//=>
// 2
// Promise { <pending> }
// 6
// 4
function sum(a, b) {
return a + b;
}
function sumPromise(a, b) {
const promise = new Promise((resolve, reject) => {
if (isNaN(a) || isNaN(b))
reject('Invalid numbers');
else
setTimeout(() => resolve(a + b), 200);
});
return promise
}
console.log(sum(1, 1));
console.log(sumPromise(2, 2));
sumPromise(2, 2)
.then((res) => console.log(res))
.catch((err) => console.log(err));
console.log(sum(3, 3));
//=>
// 2
// Promise { <pending> }
// 6
// 4
Chaining
function sumPromise(a, b) {
const promise = new Promise((resolve, reject) => {
if (isNaN(a) || isNaN(b))
reject('Invalid numbers');
else
setTimeout(() => resolve(a + b), 200);
});
return promise
}
sumPromise(2, 2)
.then((res) => sumPromise(res, 2))
.then((res) => console.log(res))
.catch((err) => console.log(err)); //=> 6
function sumPromise(a, b) {
const promise = new Promise((resolve, reject) => {
if (isNaN(a) || isNaN(b))
reject('Invalid numbers');
else
setTimeout(() => resolve(a + b), 200);
});
return promise
}
sumPromise(2, 2)
.then((res) => sumPromise(res, 2))
.then((res) => console.log(res))
.catch((err) => console.log(err)); //=> 6
Async & Await
function sumPromise(a, b) {
const promise = new Promise((resolve, reject) => {
if (isNaN(a) || isNaN(b))
reject('Invalid numbers');
else
setTimeout(() => resolve(a + b), 200);
});
return promise
}
async function sumAsync(a, b) {
const sleep = (m) => new Promise(r => setTimeout(r, m))
if (isNaN(a) || isNaN(b)) {
throw new Error('Invalid numbers');
} else {
await sleep(200);
return a + b;
}
}
async function result() {
try {
const res = await sumPromise(2, 2);
return await sumAsync(2, res);
} catch (error) {
if (error instanceof Error) {
console.log(error.message);
} else {
console.log(err);
}
}
}
result().then((res) => console.log(res)); //=> 6
function sumPromise(a, b) {
const promise = new Promise((resolve, reject) => {
if (isNaN(a) || isNaN(b))
reject('Invalid numbers');
else
setTimeout(() => resolve(a + b), 200);
});
return promise
}
async function sumAsync(a, b) {
const sleep = (m) => new Promise(r => setTimeout(r, m))
if (isNaN(a) || isNaN(b)) {
throw new Error('Invalid numbers');
} else {
await sleep(200);
return a + b;
}
}
async function result() {
try {
const res = await sumPromise(2, 2);
return await sumAsync(2, res);
} catch (error) {
if (error instanceof Error) {
console.log(error.message);
} else {
console.log(err);
}
}
}
result().then((res) => console.log(res)); //=> 6
Editar esta página