Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Contoh penggunaan promise

<!doctype html>
<html>
<head><title>Promise</title><meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
    // fungsi untuk melakukan request AJAX dan mengembalikan objek Promise
    function getUsers(url) {
      return new Promise(function(resolve, reject) {
        $.ajax({
            url: url,
            method: 'get'
        }).done(function(hasil) {
            // simpan hasil dari AJAX ke callback 'resolve' dari Promise
            // untuk kemudian nanti dipakai oleh fungsi '.then'
            resolve(hasil);
        });
      })
    }
    // fungsi untuk mengubah data JSON ke list HTML
    function ubahDataKeHTML(datauser) {
        var html = '<ul>';
        $.each( datauser, function( key, value ) {
            html += '<li>'+value.name+' - '+value.email+'</li>'
        });
        html += '</ul>';
        return html;
    }
    // panggil fungsi 'getUsers' dan jalankan fungsi '.then'
    // argumen dari fungsi '.then' adalah sebuah callback dengan argumen 'hasil'
    // yang berisikan objek JSON hasil AJAX
    getUsers('https://jsonplaceholder.typicode.com/users').then(function(hasil) {
        console.log(hasil);
        console.log('Janji telah dipenuhi!');
        var datauserHTML = ubahDataKeHTML(hasil);
        // tampilkan data user
        $(document).ready(function() {
            $('.container').html(datauserHTML);
        });
    });
</script>
</head>
<body>
    <div class="container"></div>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: jtml cdn enter 
Javascript :: cantsee auto complete for node jsmodules in vs code 
Javascript :: reading data from link in javascript 
Javascript :: spreadsheetapp resize column widths 
Javascript :: I want to enable textbox when checkbox is checked in jquery or javascript 
Javascript :: browser app get screen siwe 
Javascript :: filter last object of recursive array using javascript 
Javascript :: enable bootrstrap duellistbox from my own js 
Javascript :: khai bao bien js 
Javascript :: use only dispatch from useContext 
Javascript :: random height fallling object in js 
Javascript :: a complex label expression before a colon must be parenthesized 
Javascript :: ubicar escrol en el final js 
Javascript :: three movimiento js 
Javascript :: how to generate debug build in react native 
Javascript :: tower defense bullet following enemy with range javascript 
Javascript :: dummy servers using nodejs 
Javascript :: switch case in jsx 
Javascript :: ** in javascript 
Javascript :: column chart in js 
Javascript :: Function Recurser / Infinit Calling 
Javascript :: js how to find not unic values in array 
Javascript :: how to add datepicker in appended input 
Javascript :: onclick reset 
Javascript :: Use ChainLink Feed Registry 
Javascript :: how to put value in arrar 
Javascript :: javascript call function from event handler es6 
Javascript :: useRef is not working with custom compnents 
Javascript :: add link in react table to specific column 
Javascript :: currentContract.transferFrom is not a function 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =