Asynchronous Calls and Recursion with Node.js

后端 未结 6 704
[愿得一人]
[愿得一人] 2021-02-09 10:34

I\'m looking to execute a callback upon the full completion of a recursive function that can go on for an undetermined amount of time. I\'m struggling with async issues and was

6条回答
  •  被撕碎了的回忆
    2021-02-09 11:24

    With ES6, 'es6-deferred' & 'q'. You could try as following,

    var Q = require('q');
    var Deferred = require('es6-deferred');
    
    const process = (id) => {
        var request = new Deferred();
    
        const ids =//do something and get the data;
        const subPromises = ids.map(id => process(id));
    
        Q.all(subPromises).then(function () {
            request.resolve();
        })
        .catch(error => {
            console.log(error);
        });
    
        return request.promise
    }
    
    process("testId").then(() => {
        console.log("done");
    });
    

提交回复
热议问题