JavaScript의 Promise Chaining 솔루션
⇒ 이 Async Await는 Callback Hell과 Promise Chaining Hell 문제를 해결합니다. Async 함수는 Promise standalone 😎을 반환해야 합니다.
⇒ Async Await의 예.
→ 구현.
// Example of Async Await..
async function getUserName(username){
    const mainUser = await get(`/users?username=${username}`);
    const posts = await get(`/posts?user_id=${mainUser.id}`);
    const comments = await get(`/comments?post_id=${posts[0].id}`);
    const user = await get(`/users?username=${comments[0].username}`);
    console.log(user);
}
getUserName('Mosammat Akhi');
Basically the Async Await is the system where you can see the Asynchronous code just like same as the Synchronous code. 💁🏻 We declared this complex Callback Hell and Promise .this chaining Hell with simple like Synchronous style of Asynchronous Task 🙄.
→ 따라서 Error를 처리하면 보고 관리할 수 있습니다.
// Example of Async Await..
async function getUserName(username){
    try {
            const mainUser = await get(`/users?username=${username}`);
            const posts = await get(`/posts?user_id=${mainUser.id}`);
            const comments = await get(`/comments?post_id=${posts[0].id}`);
            const user = await get(`/users?username=${comments[0].username}`);
            console.log(user);
    } catch (ERR) {
        console.log('ERR! when try to fetching data -- ', ERR);
    }
}
getUserName('Rakhiyaatul Kubra');
This code can handle or see the Error when you needs like Promise .catch chain. We can use Try-Catch block instead of Promise.
그러나 Promise 없이는 Async Await 작업을 이해할 수 없습니다.
Async Await의 실생활 예.
/// Real World Example with Fake REST API..
// Example of Async Await.
const Axios = require('axios').default;
const USERS_URL = 'https://jsonplaceholder.typicode.com/users';
const POSTS_URL = 'https://jsonplaceholder.typicode.com/posts';
const COMMENTS_URL = 'https://jsonplaceholder.typicode.com/comments';
async function getCommentByUser(username){
    try {
        const { data: mainUser } = await Axios.get(`${USERS_URL}?username=${username}`);
        const { data: posts } = await Axios.get(`${POSTS_URL}?userId=${mainUser[0].id}`);
        const { data: comments } = await Axios.get(`${COMMENTS_URL}?postId=${posts[0].id}`);
        const firstComment = comments[0];
        console.log(firstComment);
    } catch (error){
        console.log('ERR! from -- ', error);
    }
}
getCommentByUser('Samantha');
⇒ Async Await의 또 다른 실제 예.
Async Await 함수는 Promise 자체를 반환합니다. 따라서 우리는 Async Await 함수 호출의 .then .catch 연결을 만들 수 있습니다.
// Another Real Life Example..
const Axios = require('axios');
const URL = "https://jsonplaceholder.typicode.com/users";
// Async Await to get data..
async function getUsers(){
    const { data: users } = await Axios.get(URL);
    return users;
}
// Make .then .catch promise from Async Await function..
// Async function returns the Promise.
getUsers().then((users) => {
    console.log(users);
}).catch((error) => {
    console.log(error);
});
Reference
이 문제에 관하여(JavaScript의 Promise Chaining 솔루션), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/asadanik/solution-of-the-promise-chaining-in-javascript-48o6텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)