skip to Main Content

Sorry if this is extremely simple, I can’t get my head around it. I’ve seen similar questions but nothing which gets to the heart of my problem, I think. I have a simple async function:

async function isOpen() {

    var open;

    var data = $.ajax({
      //Working code here
    });

    data.done(function(dat) {
        var obj = JSON.parse(dat);
        var msg = "";

        for (var i = 0; i < obj.length; i++) {

            let closingDate = Date.parse(obj[i].close);
            let openingDate = Date.parse(obj[i].open);

            if (closingDate > Date.now() && openingDate < Date.now()) {
                open = true;
            } else {
                open = false;
            }
        }
    });

    return open;
}

I know that this code is all working – using console.log I have seen that open is always successfully assigned to true or false. So I call this async function from another async function:

async function testFunction(){

    const open1 = await isOpen();
    //More code here...

}

But open1 (in testFunction) is always undefined – even though I use await.

Any ideas what this could be?

2

Answers


  1. async function isOpen() {
    
        var open;
    
        var data = await $.ajax({
          //Working code here
        });
    
        var obj = JSON.parse(data);
        var msg = "";
    
        for (var i = 0; i < obj.length; i++) {
    
            let closingDate = Date.parse(obj[i].close);
            let openingDate = Date.parse(obj[i].open);
    
            if (closingDate > Date.now() && openingDate < Date.now()) {
                open = true;
            } else {
                open = false;
            }
        }
        return open;
    }
    

    of course, the value of open will be determined by the last obj[i]

    So you want

    async function isOpen() {
        const data = await $.ajax({
          //Working code here
        });
        const obj = JSON.parse(data);
        let msg = "";
    
        for (var i = 0; i < obj.length; i++) {
    
            const closingDate = Date.parse(obj[i].close);
            const openingDate = Date.parse(obj[i].open);
    
            if (closingDate > Date.now() && openingDate < Date.now()) {
                return true;
            }
        }
        return false;
    }
    

    Or even

    async function isOpen() {
        const data = await $.ajax({
          //Working code here
        });
    
        const obj = JSON.parse(data);
        let msg = "";
        return obj.some(({open, close}) => {
            const closingDate = Date.parse(close);
            const openingDate = Date.parse(open);
            const now = Date.now();
            return closingDate > now && openingDate < now();
        });
    }
    
    Login or Signup to reply.
  2. This is happening because you are using a callback for ajax request and not waiting for ajax request to complete, which is going to set open in isOpen. You can return a Promise to resolve this,

    function isOpen() {
      return new Promise((resolve, reject) => {
        var open;
    
        var data = $.ajax({
          //Working code here
        });
    
        data.done(function(dat) {
            var obj = JSON.parse(dat);
            var msg = "";
    
            for (var i = 0; i < obj.length; i++) {
    
                let closingDate = Date.parse(obj[i].close);
                let openingDate = Date.parse(obj[i].open);
    
                if (closingDate > Date.now() && openingDate < Date.now()) {
                    open = true;
                } else {
                    open = false;
                }
            }
           resolve(open);
        });
    
      }
    }
    

    Note: For the above code, you need to handle the logic for errors in the callback, using reject otherwise it will hang forever.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search