I'm trying to use promise in following example. I'm learning promise but my first example doesn't work. It prints "Promise didn't work".

Now, what I know is that hide() function takes 400ms to assign value to foo. That's why ajax call wouldn't get the data. So it wouldn't work.

I can resolve this by calling ajax inside the callback of hide, but I want to resolve it using Promise (I suppose this is why promise is used?).

Is there something wrong OR this is not promises are intended to do?

Here's my code:

```
var foo;
    var res;
    var Prom;
    $("button").click(function(){
        Prom = new Promise(function(resolve, reject){
            $("p").hide(function(){
                foo="YOLO";
            });
            $.ajax({
                url: 'api.php',
                type: 'post',
                data: 'name='+foo,
                success: function(result){
                    res=result;
                    resolve(res);
                },
                error: function(){
                    reject("Promise didn't work");
                }
            });

        });
        Prom.then(function(result) {
          console.log("The PHP response is: ", result);
        }, function(err) {
          console.log(err); // Error: "It broke"
        });
    });
});

```