简体   繁体   English

使用 Bluebird 处理异步函数

[英]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我一直在尝试使用 bluebird 来运行下面的代码,尽管我收到以下错误(这一定很简单,虽然我找不到它):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,然后在它完成loop_the_table 之后。

ajax_call_to_php.js ajax_call_to_php.js

function ajax_and_loop(){函数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:在我的主要 html 文件 index.html 中,文件头的一部分是:

<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 .这里下载 bluebird.js 的地方 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:例如,表达式“require”在节点环境中是有效的,但您想在浏览器中工作,因此请尝试先加载 bluebird.js,然后加载您的自定义脚本:

<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.并删除所需的表达式,因为会加载 bluebird。

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更新我使用 bluebird 和 jquery 在我的 github 帐户上创建了一个示例来模拟您的环境: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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