最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJs forEach request-promise wait for all promises before returning - Stack Overflow

programmeradmin0浏览0评论

Problem is I'm not able to get the promises to return anything. they.. just e empty.

Every answer I see here on SO is telling me to do just this, though for some reason this is not working. I'm at my wits end, pulling hair and smashing keyboards; Can someone pin-point my dumbness?

var q = require('q');
var request = require('request-promise'); // 

function findSynonym(searchList) {
    var defer = q.defer();
    var promises = [];
    var url = ".php?word=%word%&language=en_US&output=json&key=awesomekeyisawesome";
    var wURL;
    searchList.forEach(function(word){
        wURL = url.replace('%word%',word);
        promises.push(request(wURL));
    });

    q.all(promises).then(function(data){
        console.log('after all->', data); // data is empty
        defer.resolve();
    });

    return defer;
}
var search = ['cookie', 'performance', 'danger'];

findSynonym(search).then(function(supposedDataFromAllPromises) { // TypeError: undefined is not a function [then is not a function]
    console.log('->',supposedDataFromAllPromises); // this never happens
});

Problem is I'm not able to get the promises to return anything. they.. just e empty.

Every answer I see here on SO is telling me to do just this, though for some reason this is not working. I'm at my wits end, pulling hair and smashing keyboards; Can someone pin-point my dumbness?

var q = require('q');
var request = require('request-promise'); // https://www.npmjs./package/request-promise

function findSynonym(searchList) {
    var defer = q.defer();
    var promises = [];
    var url = "http://thesaurus.altervista/service.php?word=%word%&language=en_US&output=json&key=awesomekeyisawesome";
    var wURL;
    searchList.forEach(function(word){
        wURL = url.replace('%word%',word);
        promises.push(request(wURL));
    });

    q.all(promises).then(function(data){
        console.log('after all->', data); // data is empty
        defer.resolve();
    });

    return defer;
}
var search = ['cookie', 'performance', 'danger'];

findSynonym(search).then(function(supposedDataFromAllPromises) { // TypeError: undefined is not a function [then is not a function]
    console.log('->',supposedDataFromAllPromises); // this never happens
});
Share Improve this question asked Sep 11, 2015 at 11:12 MoshMageMoshMage 5065 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You're returning the Deferred object defer, which does not have a .then method, instead of the Promise object defer.promise.

But anyway, that's the deferred antipattern, there's no need of using deferreds here. Just return the promise that Promise.all gets you:

function findSynonym(searchList) {
    var url = "http://thesaurus.altervista/service.php?word=%word%&language=en_US&output=json&key=awesomekeyisawesome";
    var promises = searchList.map(function(word) {
        return request(url.replace('%word%', word));
    });
    return q.all(promises).then(function(data){
        console.log('after all->', data); // data is empty
        return undefined; // that's what you were resolve()ing with
    });
}

So, turns out I was resolving the promises or something something. returning the q.all() worked pretty well :)

function findSynonym(searchList) {
    var promises = [];

    var url = "http://thesaurus.altervista/service.php?word=%word%&language=en_US&output=json&key=REDACTED";
    var wURL;

    searchList.forEach(function(word){
        wURL = url.replace('%word%',word);
        promises.push(request({url:wURL}));
    });

    return q.all(promises);

}
var search = ['cookie', 'performance', 'danger'];

findSynonym(search)
            .then(function(a){
            console.log('->',a);
        });
发布评论

评论列表(0)

  1. 暂无评论