Promise in JavaScript Example

Pugazhenthi
function doFirst() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: 'https://servername.com/end-point/',
            method: 'POST',
            data: { key: 'value' },
            dataType: 'json',
            success: function(data) {
                resolve(data);
            },
            error: function(error) {
                reject(error);
            }
        });
    });
}
function doNext(){
//function body
//Something to do after 'doFirst()'
}
doFirst()
.then(data => {
    console.log(data);
    doNext();
})
.catch(error => {
    console.log(error);
});

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2019, RTP SYSTEMS