일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- PQ92
- remarkable
- CSS #그라데이션 #선형 #이미지위에
- 앱코
- 리마커블2
- 래퍼럴
- gethostname
- PQ92-3BWF
- 랜덤코드
- 크러시
- random-chords
- 바이낸스
- 퓨어Q9
- random chord
- 리마커블
- 웹
- 펜윅트리
- 한글
- 2934
- 당겨서새로고침
- 어퍼스트럭쳐
- 한글폰트
- 무접점키보드
- BI트리
- 한국어
- pulltoreload
- LRH식물
- LRH 식물
- 크러쉬 - 춤
- vanilla.js
- Today
- Total
toujours
chatGPT에게 물어본 promise chaining 본문
When Promises are chained by the then() method, the return values from the previous then() callback function are passed as the resolved value to the next then() callback function.
For example, consider the following code:
In this example, we create a new Promise that immediately resolves with the value 1. We then chain two then() methods. In the first then() method, we add 1 to the resolved value and return the result. In the second then() method, we log the resolved value to the console.
When the first then() callback function is executed, it returns a value of 2. This value is then passed as the resolved value to the second then() callback function, which logs the value to the console.
So in this case, the return value from the first then() callback function (2) is passed to the second then() callback function as the resolved value of the previous Promise. This is how values are passed from one then() callback function to the next in a Promise chain.
Yes, you're correct that promise.then() returns a Promise. Here's an example implementation of the then() method to help illustrate how it works:
In this example implementation, we define a then() method on the Promise prototype. The then() method takes two arguments: onResolved, which is a callback function to be executed when the Promise is resolved, and onRejected, which is a callback function to be executed when the Promise is rejected (both of these arguments are optional).
When the then() method is called on a Promise object, it saves a reference to the current Promise object (currentPromise) and creates a new Promise object (newPromise) that will be returned by the then() method.
The then() method then registers a callback function with the current Promise object using the _registerCallbacks() method. This callback function will be executed when the Promise is resolved or rejected.
When the Promise is resolved or rejected, the callback function passed to _registerCallbacks() is executed. This function then calls the onResolved or onRejected callback function provided to the then() method, passing the resolved value or rejection reason as an argument.
If the onResolved or onRejected callback function returns a Promise object, the then() method waits for that Promise object to be resolved before resolving the new Promise object (newPromise). If the callback function returns a non-Promise value, the new Promise is resolved with that value immediately.
I hope this helps illustrate how the then() method works in JavaScript Promises!