简体   繁体   中英

Use Bluebird to handle asynchronous functions

I have been trying to use bluebird in order to run the code below though i get the following error (this must be something easy, though i have not been able to find it): ReferenceError: require is not defined
What i want to do is run first the function ajax_call and after it finishes the loop_the_table.

ajax_call_to_php.js

function ajax_and_loop(){

      Promise.all(ajax_call).then(loop_the_table).then(function(val) {
    console.log(val.success);
})

   function ajax_call(){

     $(document).ready(function(){
         $.getJSON('php_side.php', function(data) {
           $(data).each(function(key, value) {
               value = value.toString();
               res = value.split(',');
               array_of_people_already_suscribed[row][column++] = res[0];
               array_of_people_already_suscribed[row][column++] = res[1];
               array_of_people_already_suscribed[row][column] = res[2];
               row++;
               column = 0;
                   console.log("kalimera oli mera");
           });
         });
       });
     }

       function loop_the_table(){
         for(var i = 0; i < row; i++){

                   console.log("kalispera oli mera");
             console.log(array_of_people_already_suscribed[i][0]);
             console.log(array_of_people_already_suscribed[i][1]);
             console.log(array_of_people_already_suscribed[i][2]);
           }  
        }  

}    

In my main html file index.html in part of the head of file is:

<script src="js/My_js/ajax_call_to_php.js"></script>
<script src="js/My_js/bluebird.js"></script>

where bluebird.js has been downloaded from here . What am i doing wrong??

In your code you put:

var fs = require("Bluebird");

and expression "require" is valid in a node environment for example, but you want to work in a browser so, try to load first bluebird.js and then your custom script:

<script src="js/My_js/bluebird.js"></script>
<script src="js/My_js/ajax_call_to_php.js"></script>

And delete the required expression because bluebird will be load.

UPDATED I created an example on my github account using bluebird and jquery to simulate your environment: https://github.com/josemato/stackoverflow/tree/master/bluebird-promises-jquery

var ajaxSingleCall = function ajaxSingleCall() {
    return new Promise(function(resolve, reject) {
        $.getJSON('php_side.php', function(data) {
            resolve(data);
        }).fail(function(e) {
            reject(e);
        });
    });
};

var ajaxCallMultiple = function ajaxCallMultiple() {
    var promises = [];

    for(var i = 0; i < 3; i++) {
        promises.push(ajaxSingleCall());
    }

    return promises;
};

var loopTheTable = function loopTheTable(data) {
    for(var i = 0; i < data.length; i++) {
        console.log('timeWait for server', data[i].timeWait);
   }
};

// Example 1: Do only one request and transform server data to array
ajaxSingleCall().then(function(data) {
    console.log('ajaxSingleCall', data);
    return [data];
}).then(loopTheTable);

// Example 2: Do a lot of requests waiting for all
Promise.all(ajaxCallMultiple()).then(function(data) {
    console.log('All multiple data:', data);
    return data;
}).then(loopTheTable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM