Async
1. Synchronous and Asynchronous
Synchronous
Waiting for specific code execution to complete before performing the next code
Asynchronous
Not waiting for specific code execution to complete, but performing the next code
2. Asynchronous JavaScript (Timer API)
setTimeout(callback, millisecond)
setTimeout(function () {
console.log('Execute after 1 second');
}, 1000);
/// 123Execute a function after a certain time
parameter: callback function to execute, time to wait before executing the callback function (milliseconds)
return value: arbitrary timer ID
clearTimeout(timerId)
Terminate
setTimeouttimerparameter: timer ID
return value: none
setInterval(callback, millisecond)
Execute a function repeatedly at regular time intervals
parameter: callback function to execute, time interval for repeatedly executing the function (milliseconds)
return value: arbitrary timer ID
clearInterval(timerId)
Terminate
setIntervaltimerparameter: timer ID
return value: none
3. Callback
One way to control code asynchronously is to utilize
callbackfunctionsHowever, as code gets longer, callback Hell occurs, making it complex and reducing readability
4. Promise
new Promise
Promiseis a class, so create aPromiseobject using thenewkeywordPromisetakes a callback function that performs asynchronous processing as an argument, and this callback function receivesresolveandrejectfunctions as argumentsWhen a
Promiseobject is created, the callback function is automatically executedIf the code is processed well, call the
resolvefunction, and if an error occurs, call therejectfunction
Promise Object's Internal Properties
The Promise object returned by
new Promisehasstateandresultinternal propertiesHowever, cannot access directly and must use
.then,.catch,.finallymethods to access
State
Default state is
pending. If the callback function performing asynchronous processing works well β changes tofulfilled, if error occurs βrejected
Result
Default state is
undefined. If the callback function performing asynchronous processing works well andresolve(value)is called β changes to value, if error occurs andreject(error)is called β changes toerror
Then
If the code written in the callback function is processed well, call the
resolvefunction β can access through.thenmethodIf the value returned in
.thenis aPromise, receive thePromise's internal propertyresultas the argument of the next.then's callback functionIf it's not a
Promise, can receive the returned value as the argument of.then's callback function
Catch
When an error occurs in the code written in the callback function, call the
rejectfunction and can access through.catchmethod
Finally
Can access through
.finallymethod regardless of whether the code written in the callback function is processed normally
Promise Chaining
When asynchronous tasks need to be performed sequentially
Promise all()
Used when you want to process multiple asynchronous tasks simultaneously
Takes an array as an argument, and if all the code written in the callback functions of all
Promisein that array is processed normally, stores the results in an array and returns a newPromiseAdditionally, if any of the
Promisein the argument array encounters an error, immediately terminates regardless of the state of the remainingPromise
5. Async, Await
Can write complex
Promisecode concisely usingasyncandawaitUse
asyncbefore a function and use theawaitkeyword only insideasyncfunctions: code written this way runs the next code after the code with theawaitkeyword runs
6. fetch API
fetch API
Role of receiving information from a specific URL
This process is asynchronous, so it may take some time depending on the situation
7. Axios
A library that performs a similar role to fetch API
An HTTP asynchronous communication library that utilizes Promise API for browsers and Node.js
Easier to use than Fetch API with additional features included
A third-party library that requires installation
Automatically converts to JSON data format
GET Request
POST Request
Last updated