-
-
Notifications
You must be signed in to change notification settings - Fork 654
Added Promises content in introduction & about #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
a365069
45ff1fc
afa309e
b645030
4aa1eb9
84653c6
e55ebed
7e761d9
bd39306
d125fe0
7cde218
aad917f
521cf3c
58362a8
80a9ce3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,3 +1,146 @@ | ||||||
| # About | ||||||
|
|
||||||
| TODO: add information on promises concept | ||||||
| The [Promise][promise-docs] object represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. | ||||||
|
|
||||||
| A Promise is in one of these states: | ||||||
|
|
||||||
| - **pending**: initial state, neither fulfilled nor rejected. | ||||||
| - **fulfilled**: meaning that the operation was completed successfully. | ||||||
|
||||||
| - **fulfilled**: meaning that the operation was completed successfully. | |
| - **fulfilled**: meaning that the operation was completed successfully and the result is available. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence can be removed as those methods are explained in detail below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I also delete line number 11?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think it is ok to keep it as general explanation.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend to first show the instance methods and then the class methods and naming the headers accordingly. Also you can use h3 headings instead of bold text for the sub-sections. The structure could look like this:
## Instance Methods of a Promise
### then
### catch
### finally
## Static Methods of the Promise Class
[here there should be some text with a link to all the other methods as we don't show all of them, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#static_methods]
### Promise.all
### Promise.reject
### Promise.resolve| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,104 @@ | ||
| # Introduction | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The content of this file also needs to be copied to the introduction.md file of the translation service exercise: https://github.com/exercism/javascript/blob/main/exercises/concept/translation-service/.docs/introduction.md |
||
|
|
||
| The `Promise` object represents the eventual completion (or failure) of an | ||
| The [`Promise`][promise-docs] object represents the eventual completion (or failure) of an | ||
| asynchronous operation, and its resulting value. | ||
|
|
||
| The methods [`promise.then()`][promise-then], [`promise.catch()`][promise-catch], and [`promise.finally()`][promise-finally] are used to associate further action with a promise that becomes settled. | ||
|
|
||
| For example: | ||
|
|
||
| ```javascript | ||
| const myPromise = new Promise(function (resolve, reject) { | ||
| let sampleData = [2, 4, 6, 8]; | ||
| let randomNumber = Math.ceil(Math.random() * 5); | ||
| if (sampleData[randomNumber]) { | ||
| resolve(sampleData[randomNumber]); | ||
| } else { | ||
| reject('An error occured!'); | ||
| } | ||
| }); | ||
|
|
||
| myPromise | ||
| .then(function (e) { | ||
| console.log(e); | ||
| }) | ||
| .catch(function (error) { | ||
| throw new Error(error); | ||
| }) | ||
| .finally(function () { | ||
| console.log('Promise completed'); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Methods | ||
|
|
||
| These methods are available on `Promise.prototype` | ||
|
|
||
| **then** | ||
|
|
||
| > The `.then()` method takes up to two arguments; the first argument is a callback function for the resolved case of the promise, and the second argument is a callback function for the rejected case. Each `.then()` returns a newly generated promise object, which can optionally be used for chaining.[^1] | ||
|
|
||
| ```javascript | ||
| const promise1 = new Promise(function (resolve, reject) { | ||
| resolve('Success!'); | ||
| }); | ||
|
|
||
| promise1.then(function (value) { | ||
| console.log(value); | ||
| // expected output: "Success!" | ||
| }); | ||
| ``` | ||
|
|
||
| **catch** | ||
|
|
||
| > A `.catch()` is really just a `.then()` without a slot for a callback function for the case when the promise is resolved. It is used to handle rejected promises.[^2] | ||
|
|
||
| ```javascript | ||
| const promise1 = new Promise((resolve, reject) => { | ||
| throw 'An error occured'; | ||
| }); | ||
|
|
||
| promise1.catch(function (error) { | ||
| console.error(error); | ||
| }); | ||
| // expected output: An error occured | ||
| ``` | ||
|
|
||
| **finally** | ||
|
|
||
| > When the promise is settled, i.e either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.[^3] | ||
|
|
||
| ```javascript | ||
| function findDataById(id) { | ||
| return new Promise(function (resolve, reject) { | ||
| let sampleData = [1, 2, 3, 4, 5]; | ||
| if (sampleData[id]) { | ||
| resolve(sampleData[id]); | ||
| } else { | ||
| reject(new Error('Invalid id')); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| findDataById(4) | ||
| .then(function (response) { | ||
| console.log(response); | ||
| }) | ||
| .catch(function (err) { | ||
| console.error(err); | ||
| }) | ||
| .finally(function () { | ||
| console.log('Promise completed'); | ||
| }); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| [^1]: `then`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | ||
| [^2]: `catch`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | ||
| [^3]: `finally`, MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally | ||
|
|
||
| [promise-docs]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise | ||
| [promise-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | ||
| [promise-then]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | ||
| [promise-finally]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Links can be distracting so they should be used sparingly and only if they add additional value compared to the existing content. E.g. the link to the promise docs above could be placed in the links.json instead (if its not included already). It will then render below the content of this file in the "Read More" section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a link to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
Should I also add another link to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think they are different enough so that makes sense.