[JavaScript] Promise
![[JavaScript] Promise](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1681440718511%2F3e7de4d9-2a53-4002-be46-2f6b6f3d6196.png&w=3840&q=75)
Promise
Objects that help handle asynchronous operations in a synchronous-like manner
One of
resolveorrejectmust be calledexecutor: executing a function that automatically runs when Promise created
Constructor
new Promise(executor); new Promise((resolve, reject) => { // code });
Promise State
Pending
Fulfilled: operation completed -> can proceed to next operation with
.then(callback)Rejected: operation completed
Promise.then(callback)
Becomes fulfilled state when
resolveis calledMoves to
.then()when Promise is fulfilledTakes the argument from
resolve(argument)as a parameter for the callback in.then()const resolvePromise = new Promise((resolve, reject) => { setTimeout(() => { console.log('First'); resolve('Resolve!'); }, 1000); }); resolvePromise .then((data) => { console.log('Middle'); console.log('Last'); console.log(data); }) // Print: First -> 1 sec later // Middle // Last // Resolve!
Promise.catch(callback)
Becomes rejected state when
rejectis calledMoves to
.catch()when Promise is rejectedTakes the argument from
reject(argument)as a parameter for the callback in.catch()const errorPromise = new Promise((resolve, reject) => { setTimeout(() => { console.log('First'); reject('Error!!'); // if we call reject explicitly, Promise regards the error occurred }, 1000); }); errorPromise .then(() => { console.log('Middle'); console.log('Last'); }) .catch((error) => { console.log(error); }); // Print: 'Error!!'
Promise.resolve(value)
A static method call the
then()method passing argument: value, Promise, or 'thenable' to resolveWhen a Promise returns a value, the returned value is always wrapped in a Promise
const firstPromise = Promise.resolve('First'); firstPromise.then((value) => { console.log(value); }); // Print: 'First'Can be used like Functional Programming
const firstPromise = Promise.resolve('First'); firstPromise.then(console.log); // Print: 'First'const countPromise = Promise.resolve(0); function increment(value) { return value + 1; } const resultPromise = countPromise.then(increment).then(increment).then(increment); resultPromise.then(console.log); // Print: 3
Promise.all(iterable)
A static method takes an iterable of promises as input and returns a single Promise
This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values
It rejects when any of the input's promises rejects, with this first rejection reason
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
// Expected output: Array [3, 42, "foo"]
Promise.race(iterable)
A static method takes an iterable of promises as input and returns a single Promise
This returned promise settles (completed) with the eventual state of the first promise that settles
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'one');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'two');
});
Promise.race([promise1, promise2]).then((value) => {
console.log(value);
// Both resolve, but promise2 is faster
});
// Expected output: "two"
![[코테] 그리디 문제 - 무지의 먹방 라이브](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[코테] Bfs 토마토](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[코테] Dfs 문제 유형 - 그래프 내에서 구분하여 카운트 하기](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[코테] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[코테] 여행경로](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)