如何在node.js中使用$ .when? [重复]


                            
            

                

                    

                        

                             此问题已在此处提供答案</ b>:
                            
                         </ div>
                     </ div>
                 </ div>
                        

                             对Node.js中的Promises的本地支持
                                
                                     (8个答案)
                                 </ span>
                         </ div>
                
已关闭 4年前</ span>。</ div>
             </ div>
         </ aside>

我想同时发送一个动态数量的ajax请求,并希望对它们全部进行回调(即,对最后一个返回的回调),但是我正在使用node.js。 我在这里找到了一个jQuery版本来解决
将一系列Deferred传递给$ .when()
但是如何在node.js中做到这一点?</ p>

谢谢</ p>
     </ div>

展开原文

原文

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/21564993/native-support-for-promises-in-node-js" dir="ltr">Native Support for Promises in Node.js</a>
                            <span class="question-originals-answer-count">
                                (8 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2015-11-07 21:47:51Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

I want to send a dynamic amount ajax requests at same time and want a callback to them all (i.e. the callback to the last one returning), but I am using node.js. I found a jquery version to solve it here Pass in an array of Deferreds to $.when() but how can I do this in node.js?

Thanks

</div>

2个回答


NodeJS不使用AJAX,您具有HTTP API或请求。 您可以使用Promises或 async 库来解决您要执行的操作。</ p>

解决问题的一种方法是(未经测试)</ p>

  var async = require(“ async”);
    var request = require(“ request”);
    async.map([“” https://target.com/a“,” https://target.com/b“],请求,功能(错误,数据){
       console.log(data);
    });
</ code> </ pre>
     </ div>

展开原文

原文

NodeJS doesn't use AJAX, you have the HTTP API or request. What you're trying to do can be solved with either Promises or the async library.

One way of solving the problem is this (untested):

   var async = require("async");
   var request = require("request");
   async.map(["https://target.com/a","https://target.com/b"], request, function (err, data) {
      console.log(data);
   });


Promise.all接受一个promise数组并返回一个Promise of Array。 可以使用node-fetch模块异步发出HTTP请求(AJAX实际上仅指浏览器执行的请求。)</ p>

Bluebird是一个nodejs Promise polyfill。</ p>

类似</ p>

  var fetch = require('node-fetch'),
     承诺= require('bluebird');

函数fetchUrls(URLs){
     返回Promise.all(URLs.map(function(URL){
         return fetch(URL).then(function(response){
             返回response.json();
         });
     }));
}

fetchUrls(['url1','url2'])。then(function(data){
     //在这里处理结果数据
});
</ code> </ pre>
     </ div>

展开原文

原文

Promise.all takes an array of promises and returns a Promise of Array. The node-fetch module can be used to make HTTP requests asynchronously (AJAX only really refers to browser-executed requests.)

Bluebird is a nodejs Promise polyfill.

Something like

var fetch = require('node-fetch'),
    Promise = require('bluebird');

function fetchUrls(URLs) {
    return Promise.all(URLs.map(function (URL) {
        return fetch(URL).then(function (response) {
            return response.json();
        });
    }));
}

fetchUrls(['url1','url2']).then(function (data) {
    // do stuff with the result data here
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐