Javascript/함수
[Javascript] Promise.withResolvers
슬래기
2024. 5. 18. 15:07
소개
2024년에 Promise 선언을 깔끔하게하는 WEB API Promise.withResolvers 나왔다.
정적 메서드는 생성자의 실행 프로그램에 전달된 두 매개 변수에 해당하는 Promise.withResolvers()새 개체와 이를 해결하거나 거부하는 두 개의 함수를 포함하는 개체를 반환합니다.
기존 new Promise와 비교
Promise.withResolvers()다음 코드와 정확히 동일합니다.
//new Promise
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
//Promise.withResolvers
const { promise, resolve, reject } = Promise.withResolvers();
Promise.withResolvers() - JavaScript | MDN
The Promise.withResolvers() static method returns an object containing a new Promise object and two functions to resolve or reject it, corresponding to the two parameters passed to the executor of the Promise() constructor.
developer.mozilla.org