taniarascia / comments

Comments
7 stars 0 forks source link

how-to-promisify-an-ajax-call/ #23

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

How to Promisify an Ajax Call | Tania Rascia

Let's say you have an AJAX call, and some other function that depends on the AJAX call loading before it runs. Here's the AJAX function. And…

https://www.taniarascia.com/how-to-promisify-an-ajax-call/

willkf25 commented 4 years ago

Thank you so much!!!!! That saved me

ubald-lab commented 4 years ago

Dear Tania, Your work "How to Promisify an Ajax Call" , has helped me to take the next step !!

alexbrinkman0 commented 3 years ago

Thank you so much! I've been having trouble wrapping my mind around promises for a bit for some reason. This explanation makes sense to me. Thanks again!

LeauHic commented 3 years ago

The best article I found about this topic so far ! Thank you, it helped me a lot :)

Griefchief commented 3 years ago

One thousand thank yous. Thank you very much again!

whatfoxknows commented 3 years ago

Oh my goodness I've tried a dozen solutions and this one was exactly what I needed to get my ajax calls returned in the specific order I sent them. Thank you so much!

sudipbarmandev commented 2 years ago

Great ... :)

mikeTheMSFan commented 2 years ago

Thank you!

fhuo-ch commented 2 years ago

Hi I have a strange problem using ajax to get the ipaddress written in PHP on the server. The response is written in a new screen. Why? The new screen shows the answer from PHP. On the original screen the response from PHP is written correctly to local storage. Is the response to fast? Is my coding ok? Thanks for help - Freddy

    function doTheThing() {
        return new Promise((resolve, reject) => {
            url = "./get_ipaddress.php";
            var n = location.hostname.indexOf("localhost");
            if (n >= 0) {
                url = "get_ipaddress.php";
            }
            $.ajax({
                url: url,
                type: 'POST',
                success: function (data) {
                    resolve (data)
                },
                error: function (error) {
                    reject (error)
                }
            })
        })

    function get_ipadress() {
        //localStorage.clear("crz_ip_address");
        if (localStorage.getItem("crz_ip_address") === null) {
            doTheThing()
                .then((data) => {
                    var split = data.split(";");
                    localStorage.setItem("crz_ip_address", split[0]);
                })
                .catch((error) => {
                    alert (error);
                    alert('Network error has occurred please try again!');
                })
        }
    }
    }
Griefchief commented 2 years ago

@fhuo-ch The issue is not in the code presented, or at least I don't see it. You are triggering this either from a form (via html attributes) or some other code that is sending you where you don't want to be. Things to try:

1) comment out half of the code each time till you figure out which part of code is causing it 2) step through the code with debugger till you figure out which code is causing it (it will change the html page and generate an event in the networking tab of the debugger) 3) replace $.ajax with axios if you think ajax might be causing it (in the remote case 1 and 2 don't work) 4) read $.ajax documentation 5) check the networking and console tabs of the firefox/chrome debugger for clues. 6) try the Fetch API instead of $Ajax or axios, it's well suported now, check "caniuse dot com".

Good luck.

ericewers commented 2 years ago

Thank you! You've been a big help! I was having the hardest time getting my 2nd function to run only when the AJAX finished loading its data.

wingled22 commented 2 years ago

I tried the code written but not executing properly, so I use this instead:

function emailOnChange(e) {
        const promiseUpdateEmailExist = new Promise(function (resolve, reject) {
            $.ajax({
                url: "url_here",
                data: data_here,
                success: function (data) {
                    resolve(data)
                },
                error: function (data) {
                    reject(error)
                }
            });
        });

        promiseUpdateEmailExist.then((data) => {
           //code if success
        }).catch(function (error) {
           //code if error
        });
    }
theophany77 commented 2 years ago

Dear Tania, it works like a charm. Thank you so much ! Jean-michel A., France

zhangr4 commented 2 years ago

Hi Tania, thank you very much for this post. I am also using your VS Code theme.

david-kariuki commented 1 year ago

This is awesome, Thank you.

VongphetPSV commented 1 year ago

Many thanks Tania for sharing. This is awesome and very useful

dilaraaydin commented 1 year ago

thank you!

xIDMONx commented 1 year ago

Muchas gracias y gracias por compartir tu conocimiento y experiencia.

selimkoc commented 1 year ago

You could do the Ajax call not asynchronously by adding this option to your jQuery Ajax call: async: false If you do so, your next function will wait your Ajax call. No extra work is necessary.

klupa-gotoma commented 1 year ago

This example makes promises so clear.