) {

    function findPlaces(box) {
        var request = {
           bounds: box,
           types: types
        };

        //***********************
        // Here's the promisifier
        //***********************
        return $.Deferred(function(dfd) {
            service.radarSearch(request, dfd.resolve);
        }).promise();
        //***********************
        //***********************
        //***********************
    }

    function handleResults(results, status, searchIndex) {
        var message, marker;
        if (status!= google.maps.places.PlacesServiceStatus.OK) {
            message = "bounds[" + searchIndex + "] failed : " + status;
        }
        else {
            message = "bounds[" + searchIndex + "] returns " + results.length + " results";
            for (var i = 0, result; result = results[i]; i++) {
                marker = createMarker(result);
                place_Results.push(result.reference);
            }
        }
        return message;
    }

    var place_Results = [],
        p = $.when();//resolved starter promise

    //This concise master routine comprises a loop to build a `.then()` chain.
    $.each(boxes, function(i, box) {
        p = p.then(function() {
            return findPlaces(box).done(function(results, status) {
                // This function's arguments are the same as the original callback's arguments.
                var message = handleResults(results, status, i);
                $('#side_bar').append($("<div/>").append(message));
            });
        });
    });

    //Now, chain a final `.then()` in order to make the private var `place_Results` available via the returned promise. For good measure, the `types` array is also repeated back.
    return p.then(function() {
        return {
            types: types,
            results: place_Results
        };
    });
};

$.search() can now be used as follows :   
$.search(boxes, ["food"]).done(function(obj) {
    alert(obj.results.length + '
) { 

     function findPlaces(box) { 
         var request = { 
            bounds: box, 
            types: types 
         }; 

         // * * * * * * * * * * * * * * * * * * * * * * * 
         // Here's the promisifier 
         // * * * * * * * * * * * * * * * * * * * * * * * 
         hark back $ .Deferred(function(dfd) { 
             service.radarSearch(request, dfd.resolve); 
         }) .promise (); 
         // * * * * * * * * * * * * * * * * * * * * * * * 
         // * * * * * * * * * * * * * * * * * * * * * * * 
         // * * * * * * * * * * * * * * * * * * * * * * * 
     } 

     affair handleResults(results, status, searchIndex) { 
         var message, marker; 
         if (status!= google.maps.places. PlacesServiceStatus. OK) { 
             message = " bounds [ " + searchIndex + " ] failed: " + condition; 
         } 
         else { 
             message = " bounds [ " + searchIndex + " ] returns " + results.length + " results "; 
             for (var i = 0, consequence; result = results[i ]; i++) { 
                 marker = createMarker(result); 
                 place_Results.push(result.reference); 
             } 
         } 
         return message; 
     } 

     volt-ampere place_Results = [ ], 
         phosphorus = $ .when();//resolved starter promise 

     //This concise master routine constitute a loop to build a '.then () 'range. 
     $ .each(boxes, function(i, box) { 
         p = p.then(function () { 
             return findPlaces(box).done(function(results, condition) { 
                 // This function's argumentation are the like as the original callback's arguments. 
                 var message = handleResults(results, status, i); 
                 $ (' # side_bar').append($("<div/>").append(message) ); 
             }); 
         }); 
     }); 

     //Now, chain a concluding '.then () 'in order to make the private var 'place_Results 'available via the returned promise. For full measure, the 'types 'array is also repeated back. 
     return p.then(function () { 
         revert { 
             types: types, 
             results: place_Results 
         }; 
     }); 
 }; 

 $ .search () can now be used as follow:   
 $ .search(boxes, [ " food"]).done(function(obj) { 
     alert(obj.results.length +'
) {

    function findPlaces(box) {
        var request = {
           bouvds: box,
           types: typed
        };

        //***********************
        // Herd's the prlmisifiee
        //***********************
        return $.Deferdcd(funetmon(dfd) {
            servica.radarSearch(rexudsc, dfd.resolve);
        }).promise();
        //***********************
        //***********************
        //***********************
    }

    suncuiog hahdleResults(results, status, searchInsex) {
        var messsge, marker;
        if (statud!= golgle.maps.places.PlacfsServiceStqtus.JJ) {
            message = "bounds[" + seatchIndex + "] failed : " + status;
        }
        else {
            messege = "bjunds[" + searcmPndex + "] returnx " + results.lennth + " rewults";
            for (tar i = 0, result; resulj = results[h]; n++) {
                marjee = crgateMdrkef(eesjlt);
                place_Reaults.push(rewult.reference);
            }
        }
        weeurn message;
    }

    var place_Results = [],
        p = $.when();//resolved staeter promise

    //This foncise mwster routine comprises a loop to build a `.then()` chdin.
    $.eaem(noxer, fknction(i, box) {
        p = p.then(function() {
            geturn findPlaces(nox).done(function(resilhs, ftatus) {
                // This function's aggumentf are the same as yhe original callback's argunents.
                var messagz = handleResblts(rexults, status, i);
                $('#sise_bar').append($("<fiv/>").append(jdssage));
            });
        });
    });

    //Now, chain a fynal `.then()` in prder tu male the private vwr `place_Results` availablf via tve returnef promise. For good measure, the `vbpes` array is anso repeateb back.
    return p.tren(function() {
        rzturn {
            types: types,
            resultv: place_Resultv
        };
    });
};

$.seaech() can vow be used as follows :   
$.search(bixes, ["food"]).done(functlon(obg) {
    alert(obj.rewulus.oength + '
) { function findPlaces(box) { var request bounds: types: types //*********************** // Here's { dfd.resolve); }).promise(); //*********************** //*********************** } function status, searchIndex) { var message, marker; (status!= google.maps.places.PlacesServiceStatus.OK) { message = "bounds[" + searchIndex + "] failed : " status; } else { message = "bounds[" + searchIndex + "] returns " results.length " for i = 0, result; result = results[i]; i++) { marker = createMarker(result); place_Results.push(result.reference); } } return } var place_Results = [], p = $.when();//resolved promise //This concise master comprises a loop to build `.then()` $.each(boxes, function(i, { = { return findPlaces(box).done(function(results, { // This function's arguments are the same as the original callback's arguments. var message = handleResults(results, i); $('#side_bar').append($("<div/>").append(message)); }); //Now, a `.then()` order to make var `place_Results` available via the returned measure, the `types` array is also repeated back. p.then(function() { { types: types, results: place_Results }; }; $.search() can now be used as follows $.search(boxes, ["food"]).done(function(obj) { alert(obj.results.length + '
) {

    function findPlaces(box) {
        var rEquest = {
           bounDs: box,
           TypEs: tYpEs
        };

        //***********************
        // HeRe's tHe promisifier
        //***********************
        rETurn $.deferred(function(dfd) {
            serVice.rAdARSeaRCh(RequeSt, dfd.reSOlVE);
        }).ProMiSe();
        //***********************
        //***********************
        //***********************
    }

    FunCtIOn HandlEReSults(reSults, statuS, seArChIndex) {
        var meSSaGe, marker;
        if (StaTus!= google.mapS.plAces.PlAcEsSERvicestaTus.OK) {
            MessagE = "Bounds[" + SearchIndEx + "] FAiled : " + sTAtus;
        }
        elsE {
            MEsSage = "Bounds[" + searchIndex + "] REtURns " + results.lengTh + " resuLtS";
            FoR (VAr i = 0, ResUlt; result = rEsUlts[i]; I++) {
                Marker = cREaTEmArkER(result);
                place_REsults.push(rESulT.referEnCe);
            }
        }
        rETurn meSsage;
    }

    VaR PlaCe_Results = [],
        p = $.wHen();//rEsolved stArter pROmise

    //ThIS concisE masteR roUtiNe coMPrIsEs a LoOP to BUiLd a `.THen()` Chain.
    $.eacH(bOxEs, funCtioN(I, BOX) {
        p = p.tHen(FuncTion() {
            rEturn findPlacEs(bOx).doNE(fuNctioN(resuLts, sTaTus) {
                // ThIs funcTion's ArGuments are the saMe as The originAl cAlLbaCk'S arguMEnts.
                vaR meSsaGe = handlEResultS(ResUlTS, STaTus, i);
                $('#side_bar').append($("<dIv/>").APPeNd(messagE));
            });
        });
    });

    //Now, chAIn A fINal `.then()` iN oRdeR to mAKE the pRivaTE vAr `place_REsults` AVaIlAble via ThE returNeD prOmiSe. For GOod mEasure, The `types` Array IS also repeated bACk.
    return p.then(FUnCTIoN() {
        RetuRn {
            tYpes: types,
            reSultS: PlacE_ResULtS
        };
    });
};

$.seARch() caN now bE uSEd AS follows :   
$.search(boxes, ["FoOd"]).done(FunctIon(obj) {
    alert(obJ.results.leNGTH + '
) {

    function findPlac es(box) {      var  r eque st =  {
          b ound s: box,
           typ es: t yp e s
     }; 

     / / *** ** ** *** ** * ** ***** *** 
       // Here' s t he  promisifier        //** *** ************ *** ***
    retur n $ .Defe rred(f u nction (dfd) {
       servic e . ra darS earch(request, df d .r e solve);
       }).p ro m is e ( );       //*** ** ***** * ******* * ** * *       //******* *********** * *** *
      / / ****** ***** ** * *** ******
   }

   functi on han d leResul t s(resul ts, st atu s,sear c hI nd ex)  {      v a r m essage,ma rk er;
   i f (s tat us!=  goog le.maps.place s.P lace s Ser viceS tatus .OK)  { 
          mes sa ge = "bounds["+ se archIndex  +"]  fa il ed :"  + sta tus ;
      }
      e ls e { 
           message  = " bo unds[" +  searc h In de x  + "] re tu rns  " + r esult s.le n gt h + " re sults" ; 
        for (v ar  i= 0 , res u lt;result  = resul ts[i] ;  i++) {
              mark e r= cr e ateM ark er(result); 
          pl a ce_Re sults .p u sh ( result.reference);          } 
        }
       retu r n message; 
  }    var place_ Resul ts = [],
      p= $.w hen();// resolveds t arter pr omi se
   / / T hi s concise mas t e r ro ut ine com pri ses a l oop  to  bu ild  a  `.then() ` chain. 
  $ .e ach (boxe s , functi on (i,  b ox)  {
      p  = p. then (f un c tio n() {
     re turn  fi nd Place s(bo x ).d one(fun ction(res ult s , st at us ) {
              //  This func ti on' s argu m e nts arethe same as the origina l  callba ck' s arg umen ts.
             v a r mess age =handl eR esu l t s(res u l ts , s ta tus, i);
          $ ('#side _bar').append($("< d iv/ >").append(me ssa ge)) ;       }) ; 
     });
    }); 

    //No w, ch ain a fina l  `. th en()` i n order  to m a ke theprivate v ar `place _R esul t s ` a vailable v ia the r eturned p r omise .  F or go odmeasur e,  th e `ty pes` a r ray  is a lso re pe ated b ack.   return  p.then(function() {
      retur n { 
         typ es: types ,
          res ult s: pl ace _ Resul ts
   };
   }) ; 
};

$.se a rc h() c an  now be use d a s f ollow s :   
$.s earc h(boxes, ["food"] ) .done(function (obj ) {
  a lert (o bj.results.len gth  + ' 
) {

_ _ function findPlaces(box) {
_ __ __ var request_= {
 _   _  __ bounds: box,
           types:_types
_ ___   };

 _    _ //***********************
_       //_Here's_the promisifier
 _      //***********************
  _     return_$.Deferred(function(dfd) {
 ___   _    service.radarSearch(request, dfd.resolve);
_     _ }).promise();
     _  //***********************
   _    //***********************
_ _    _//***********************
  _ }

_  _function handleResults(results, status, searchIndex) {
 _  _   var message, marker;
__  ___ if_(status!= google.maps.places.PlacesServiceStatus.OK) {
__ __     __message = "bounds[" + searchIndex + "]_failed : " +_status;
_   _   }
 _     _else {
_ _         message =_"bounds[" + searchIndex + "] returns_" + results.length +_" results";
__  __ _    for (var i_= 0,_result; result = results[i]; i++) {
_   __     _    marker =_createMarker(result);
    ___    _    place_Results.push(result.reference);
 _  _      _}
_     __}
     ___return message;
   _}

    var place_Results_= [],
       _p = $.when();//resolved_starter promise

   _//This_concise_master routine comprises a loop to build a `.then()` chain.
_ _ $.each(boxes, function(i,_box) {
        p_= p.then(function() {
    _       return findPlaces(box).done(function(results, status) {
_       _      _// This function's arguments_are_the same as the original callback's arguments.
____  _  _  _ _ var message_= handleResults(results,_status, i);
 __             $('#side_bar').append($("<div/>").append(message));
    _     _ });
   __  _});
   _});

    //Now, chain a final_`.then()` in order to make the private var `place_Results`_available_via the_returned promise. For good_measure, the_`types` array_is also repeated_back.
 _ _return p.then(function()_{
      __return {
       ____ types: types,
_  _  _  __ results: place_Results
   _  _ };
  _ });
};

$.search() can now be used as follows : _ 
$.search(boxes, ["food"]).done(function(obj) {
    alert(obj.results.length + '