-3

i have implement the java script for request data to the server using ajax (xhr).

but somehow, the loop are not promise.

i tried to implement Promise but alway failed.

this is the javascript what i want to convert into promise

    var resulA = ajaxA(a);
    For(resultA){
       var resultB = ajaxB(url?param1=resultA.id);
       for(resultB){

           var resultC = ajaxC(url?param1=resultA.id¶m2=resultB.id);
           for(resultC){
           }

        } 
    }

could you please give me a guide how to turn the javascript above into Promise ??

edited: here the java script that i want to use promise

var xhr = $.getJSON('${pageContext.request.contextPath}/someURL.action?');
        console.log("after load ajax");
xhr.done(function(data){

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


         var xhrPolygon =            $.getJSON('${pageContext.request.contextPath}/Koordinat/getKoordinateAja.action?idArea='+data[i].id);
                      xhrPolygon.done(function(resultData){
if(resultData.length>0){
                             for (var j = 0; j < resultData.length; j++) {
                                    var koordinat = [resultData[j].areaLng,resultData[j].areaLat];
                                    console.log(koordinat);
                             }
                            }
                        }); 



}
});
vian qu
  • 21
  • 4
  • 1
    You should have some errors showing up in your console. – Script47 Aug 15 '17 at 09:58
  • 4
    Please note: Java and Javascript are not the same language, in the same way that Austria and Australia are not the same country. – PeterMader Aug 15 '17 at 09:59
  • Probably a duplicate of [*How do I return the response from an asynchronous call?*](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call); some of the answers there talk about how to handle a bunch of promises either in series (one after another) or in parallel (all at once, waiting for them all to finish). – T.J. Crowder Aug 15 '17 at 10:01
  • @PeterMader thankyou for correction, my wrong. – vian qu Aug 15 '17 at 10:02
  • How are we supposed to help you if we don't even know what it's supposed to do? [mcve]s shouldn't contain syntax errors. If `ajaxA` is some kind of pseudo-code, please explain what it does. Does it return a promise? – PeterMader Aug 15 '17 at 10:03
  • @Script47 : I don't even know how the Promise get data from previous promise and used it for parameter to get data to the web server using ajax in the next promise. – vian qu Aug 15 '17 at 10:04

1 Answers1

0

javascript xhr isn't build on top of promises, but the new fetch does here you have documentation https://github.github.io/fetch/

if you need to create a promise here you have some code

let promise = new Promise((resolve, reject) => {
   if(success){
     resolve();
   }
   else{
    reject()
}
});

promise.then(r => console.log(r), e => console.log(e));
  • then, more than 1 promise, let say Promise A and Promise B, promise B required some of properti on result from Promise A, how to pass the result of Promise A to te Promise B and use it from promise B function. – vian qu Aug 15 '17 at 10:23
  • promise.then( r => { //do something console.log(r, 'A'); return r; }) .then( r => { //do something console.log(r, 'B'); return r; }) .then( r => { //do something console.log(r, 'c'); return r; }); – Pedro Lescano Aug 16 '17 at 01:15
  • you need to return the response of the promise – Pedro Lescano Aug 16 '17 at 01:19