Recursive Promises Not Returning

后端 未结 2 1242
走了就别回头了
走了就别回头了 2020-12-02 02:43

I have a recursive function like so

function missingItemsPromise() {
    return new Promise(resolve => {
        if (missingItems == 0) {
            con         


        
2条回答
  •  温柔的废话
    2020-12-02 03:06

    you are missing the return statement inside the else condition

    function missingItemsPromise() {
        return new Promise(resolve => {
            if (missingItems == 0) {
                console.log('resolves');
                console.log(products);
                return resolve();
            } else {
                page++;
                url = getUrl(id, page);
                http.get(url, function(xres) {
                    xres.setEncoding('utf8');
                    xres.on('data', function (xtraBody) {
                        console.log('calling');
                        var xtraJson = JSON.parse(xtraBody);
                        var xtraProducts = xtraJson['products'];
                        products = products.concat(xtraProducts);
                        productsLength = products.length;
                        missingItems = total - productsLength;
                       return  missingItemsPromise(); //this is the line that changes
                    });
                });
            } 
        });
    };
    

提交回复
热议问题