mirror of
https://github.com/xfarrow/javascript-tutorials.git
synced 2025-04-15 10:57:21 +02:00
update
This commit is contained in:
parent
04e200e406
commit
6f7bf89604
@ -46,6 +46,9 @@ function entryPoint () {
|
||||
asynchronous function, such as time, such as
|
||||
setTimeout(callback, delay).
|
||||
|
||||
Callbacks are not asynchronous by nature, but can be used
|
||||
for asynchronous purposes.
|
||||
|
||||
The problem is that this makes the code harder to read
|
||||
so modern JS is written using Promises and async/await
|
||||
constructs.
|
||||
|
@ -12,6 +12,10 @@
|
||||
- Pending (the operation is being processed)
|
||||
- Fullfilled (the operation has completed successfully, resolve has been called)
|
||||
- Rejected (the operation has not completed successfully, reject has been called)
|
||||
|
||||
A promise represents the completion of a (likely) asynchronous function. It is an object
|
||||
that might return a value in the future. It accomplishes the same basic goal as a
|
||||
callback function, but with many additional features and a more readable syntax.
|
||||
*/
|
||||
const promise = new Promise(function (resolve, reject) {
|
||||
setTimeout(() => resolve('done'), 5000);
|
||||
@ -20,7 +24,7 @@ const promise = new Promise(function (resolve, reject) {
|
||||
/*
|
||||
The first argument of .then is a function that runs when the promise is resolved and receives the result.
|
||||
The second argument of .then is a function that runs when the promise is rejected and receives the error.
|
||||
The function passed to "then()" is put in the event loop queue!
|
||||
The function passed to "then()" is put in the queue!
|
||||
*/
|
||||
promise.then(
|
||||
result => console.log('The operation was successful. It returned ' + result),
|
||||
|
Loading…
x
Reference in New Issue
Block a user