Promises are always asynchronousPromise’s callback always be executed after synchronous code
const promise = Promise.resolve();
promise.then(() => console.log(‘async’));
console.log(‘sync’);
Chained promises return new promises
Promise then returns a new promise each time its invoked
const p = Promise.resolve();
const chain = p.then(() => {});
console.log(p === chain);
Forever then()
Promises support infinite chaining
Promise.resolve(1)
.then(value => value + 1)
.then(value => value + 1)
.then(value => console.log(value));
You can convert callbacks to promises
You can wrap older code which uses callback in promise to work with modern async/await
function asyncOperation(callback) {
setTimeout(() => callback(null, ‘Im a callback’), 1000);
}
const promisified = () => new Promise((resolve, reject) => {
asyncOperation((err, result) => {
if (err) reject(err);
else resolve(result);
});
});
promisified().then(result => console.log(result));
Promise.resolve() doesn’t always create a new promise
If you pass a non-Promise value, Promise.resolve() wraps it into a resolved promise. But if you pass a promise, it just returns that same promise.
const p1 = Promise.resolve(‘Hello’);
const p2 = Promise.resolve(p1);
console.log(p1 === p2);
You can handle errors anywhere in the chain
Promise.reject(‘Error!’)
.then(() => console.log(‘This will not run’))
.then(() => console.log(‘This will also not run’))
.catch(err => console.log(‘Caught:’, err))
.then(() => console.log(‘This will run’));
finally() doesn’t pass values
The finally() method doesn’t receive or modify resolved values. It’s used for cleaning up resources and runs whether the promise resolves or rejects.
Promise.resolve(‘resolved’)
.then(value => console.log(value))
.finally(() => console.log(‘Cleanup’))
Promises are immutable once settled
Once a promise is settled (resolved or rejected), its state is immutable. It can’t be changed after that, even if you try to resolve/reject it again.
const p = new Promise((resolve, reject) => {
resolve(‘First’);
resolve(‘Second’);
});
p.then(value => console.log(value));
You can chain catch() to handle specific errors
Promise.reject(‘type C error’)
.catch(err => {
if (err === ‘type A error’) console.log(‘handle type A’);
throw err;
})
.catch(err => {
if (err === ‘type B error’) console.log(‘handle type B’);
throw err;
})
.catch(err => {
if (err === ‘type C error’) console.log(‘handle type C’);
throw err;
})
You can use await with non-promise values
async function demo() {
const result = await 42;
console.log(result);
}
demo();
That’s it! Thank you for reading this far. Till next time!
Source: hashnode.com