Home>

How to rewrite this code without Async: False

$ ('. Cart-Item'). Removeclass ('in-Cart');
  Cart_List.map (Item= >
 {
    $ (`.cart-item [data-cart-id="$ {item.id} "]`) .adclass ('in-Cart');
    if ($ `.cart-item [Data-Cart-id="$ {item.id} "]`) .length== 0) {
      $.ajax ({
        url: '/core/action/cart/req.php',
        TYPE: 'POST',
        ASYNC: FALSE,
        Data: {
          Items: Item.
        }
        SUCCESS: FUNCTION (DATA) {
          Cart.Render (DATA);
        }
      })
    }
  });
      $ (. Cart-Item '). Each (function () {
        If (! $ (this) .Hasclass ('in-Cart')) {
          $ (this) .adclass ('Remove');
        }
      });

How to expect the answer from AJAX before a new iteration goes? With the ASYNC parameter: False everything works as I need, but it is very bad practice and it is not recommended to do so. Please help figure out

  • Answer # 1

    You can create an asynchronous iconic object using an asynchronous generator, and then go through it in an asynchronous cycle. For /await. .

    (Async FUNCTION AddImages () {
        Let image_urls= [
            "https://image.freepik.com/free-vector/linear-flat-wedding-monograms_52683-64319.jpg"
            "https://image.freepik.com/free-vector/flat-car-poster-with-photo-horizontal_52683-64510.jpg"
        ];
        ASYNC FUNCTION * LOADIMAGES (URLS) {
            For (Let URL of URLs) {
                Console.log ('Loading:' + image_urls.Indexof (URL));
                Let image= new image ();
                image.src= url;
                image.onload= Yield Function (Container) {
                    Console.log (Image_urls.indexof (URL) + 'Has Loaded');
                    Container.APPEND;
                }
            }
        }
        For Await (Let F of Loadimages (Image_urls)) {
                F (document.body);
        }
    }) () 

    If literally on the topic of data request, then implementing through fetch :

    const urls= [
        'https://jsonplaceholder.typicode.com/users/1',
        'https://jsonplaceholder.typicode.com/users/2'
    ];
    (ASYNC FUNCTION PLACEDATA () {
        ASYNC FUNCTION * GetJSON (URLS) {
            For (Let URL of URLs) {
                Console.log ('Sending Request for User' + (URLS.INDEXOF (URL) + 1));
                Let Data= Await Fetch (URL);
                console.log ('Waiting for User' + (URLS.INDEXOF (URL) + 1));
                yield data.json ();
            }
        }
        For AWAIT (LET JSON OF GETJSON (URLS)) {
                Console.log (JSON);
        }
    }) () 
  • Answer # 2

    I will brief. Can rewrite with ASYNC /AWAIT. . For details or in the search engine, because the topic is very extensive to write it in response, or here: How to return a value from an event or from the callback function? Or at least wait for their end

    There, too, there is lighting of this syntax, but very scarce and hardly something will understand.


    But in fact, you do not need ajax query in the cycle. It is enough to collect all the data and all the scope with one request to send them to the processing server. And then then process the result received from the server. One Ajax queries with a data array is enough.