【问题标题】:How can I wait for a response inside json array iteration?如何在 json 数组迭代中等待响应?
【发布时间】:2018-01-25 11:32:27
【问题描述】:

我有一个这样的json

{"queueDetails":
    [
        {
            "filepath":"video1.mp4",
            "format":{"Video":"mp4"},
            "formatpath":{"Video":"http://localhost/test"},
            "outputpath":{"Video":"D:/video/filename_video1.mp4"}
        },
        {
            "filepath":"video2.mp4",
            "format":{"Video":"mp4"},
            "formatpath":{"Video":"http://localhost/test"},
            "outputpath":{"Video":"D:/video/filename_video2.mp4"}
        },
    ]
}

在这里我想迭代它,我想获取第一个数组元素并提交给另一个函数并等待它的状态。当第一个过程完成后,我想选择下一个数组元素并对数组的其余元素执行相同的操作。

我们如何在 node js 中实现这一点?

如何在迭代内部实现等待逻辑?

我的目标是使用这个 json 进行一项一项的操作。

【问题讨论】:

标签: javascript arrays json node.js


【解决方案1】:

您可以使用 async.forEachLimit。

 async.forEachLimit(JSONArray, 1, function(singleObject, funCallback) 
       {
            //do your task with singleObject and than do funCallback
            //example call mongo query and then callback
             Collection.find(singleObject.id, function(err,dbResult) 
               {  
                 console.log(dbResult);
                 funCallback()
               });

        }, function() {
            console.log("complete");
        })

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 2020-11-19
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多