# new Promise(응용)2

### new Promise & ajax 사용하기

```javascript
$(function(){
    var datas;
    // Promise 정의1
    var fileA = function(){
        var promise = new Promise(function(resolve, reject){
            $.ajax('https://firebaseID.firebaseio.com/items/api.json')
            .done(function(response){
                resolve(response);
            }).fail(function(){
                reject('에러 메시지');
            });
        });
        return promise;
    };

    var templates = (datas) => {  
        datas.forEach((data) => {
            var template = '';
            template += '<li class="main__card wow fadeIn">';
            template += '<div class="main__card--bg">';
            template += '<figure>';
            template += '<img class="thumb card__img" src="'+data.img+'" alt="상품이미지" />';
            template += '<figcaption>';
            template += '<h2 class="card__title">'+data.title+'</h2>';
            template += '<p class="card__text">'+data.text+'</p>'; 
            template += '<div class="card__footer">';
            template += '<p class="card__text card__text--secondary">'+data.name+'</p>';
            template += '</div> </figure> <figcaption> </div> </li>';
            $('body').append(template); 
        });
    };


    //위에서 정의한 내용이 Promise 수행이 되고 나서(then) 실행을 한다.
    fileA().then((data) => {
        datas = data;
        templates(datas);    //response 값 출력
    });
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://triplexlab.gitbook.io/vanilla-js/practice/new-promise-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
