You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -972,13 +972,13 @@ const xFetcherPromise = new Promise( // Create promise using "new" keyword and s
972
972
)
973
973
```
974
974
975
-
As seen in the above sample, the Promise object takes a function which takes two parameters **resolve** and **reject**. Those parameters are functions which when called are going to move the promise *pending* state to respectively a *fulfilled* and *rejected* state.
975
+
As seen in the above sample, the Promise object takes an *executor* function which takes two parameters **resolve** and **reject**. Those parameters are functions which when called are going to move the promise *pending* state to respectively a *fulfilled* and *rejected* state.
976
976
977
-
But at the moment, the promise has not been used but only has been declared and stored into *xFetcherPromise* variable! So it doesn't have a current state.
977
+
The promise is in pending state after instance creation and it's *executor* function is executed immediately. Once one of the function *resolve* or *reject* is called in the *executor* function, the promise will call its associated handlers.
978
978
979
-
##### Use the promise
979
+
##### Promise handlers usage
980
980
981
-
To use the promise, we do the following:
981
+
To get the promise result (or error), we must attach to it handlers by doing the following:
982
982
983
983
```js
984
984
xFetcherPromise
@@ -990,11 +990,11 @@ xFetcherPromise
990
990
})
991
991
```
992
992
993
-
```.then``` is a method that once called will put the xFetcherPromise in **pending** state. When called, the promise body runs, and in this case, an Ajax call is being done.
993
+
If the promise succeeds, *resolve* is executed and the function passed as ```.then``` parameter is executed.
994
994
995
-
If it succeeds, *resolve* is called and the function passed as ```.then``` parameter is executed.
995
+
If it fails, *reject* is executed and the function passed as ```.catch``` parameter is executed.
996
996
997
-
If it fails, *reject* is called and the function passed as ```.catch``` parameter is executed.
997
+
> **Note :** If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached. [(Ref: MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Description)
0 commit comments