nodejs异步:多个依赖的HTTP API调用 [英] nodejs async: multiple dependant HTTP API calls

查看:67
本文介绍了nodejs异步:多个依赖的HTTP API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个项目,该项目涉及向不同的API发出多个HTTP GET请求,每个请求都需要最后一个的信息.我正在尝试避免嵌套回调和计数器地狱,并且一直在尝试使其与 async 模块一起使用.

I'm working on a project that involves making multiple HTTP GET requests to different APIs, each requiring information from the last. I'm trying to avoid nested-callaback-and-counter-hell, and have been trying to get it working with the async module.

这是我需要做的:我有一个1..n课程标识符数组( ['2014/summer/iat/100/d100','2014/spring/bisc/372/d100'] ).对于数组中的每个课程,我需要通过HTTP GET获取其课程大纲.

This is what I need to do: I have an array of 1..n course identifiers (['2014/summer/iat/100/d100', '2014/spring/bisc/372/d100']). For each course in the array, I need to fetch its course outline via a HTTP GET.

结果轮廓如下所示:

{
  "info": {
    "nodePath": "2014/spring/bisc/372/d100",
    "number": "372",
    "section": "D100",
    "title": "Special Topics in Biology",
    "term": "Spring 2014",
    "description": "Selected topics in areas not currently offered...",
    "name": "BISC 372 D100",
    "dept": "BISC",
 },
 "instructor": [
    {
      "lastName": "Smith",
      "commonName": "Frank",
      "phone": "1 555 555-1234",
      "email": "franksmith@school.edu",
      "name": "Frank Smith",
      "roleCode": "PI"
    },
    {
      "lastName": "Doe",
      "commonName": "John",
      "phone": "1 555 555-9876",
      "email": "johndoe@school.edu",
      "name": "John Doe",
      "roleCode": "PI"
    }
  ]
}

(省略了一堆不相关的字段)

(a bunch of non-relevant fields omitted)

每个大纲对象可能包含一个 instructor 属性,该属性是课程的0..n个教师对象的数组.对于 instructor 数组的每个成员,然后我需要调用另一个API来获取其他数据.当该调用返回时,我需要将其插入正确的教师对象中.

Each outline object may contain an instructor property which is an array of 0..n instructor objects for the course. For each member of the instructor array, I need to then call another API to get additional data. When that call returns, I need to insert it into the right instructor object.

最后,当一切都完成后,数据将传递到模板进行快速表达,并返回给客户端.

Finally, when everything is done, the data gets passed to a template for express to render and return to the client.

我尝试使用 async 使其工作,并且在仅通过其中一个教师资料进行概念验证时,在 async.waterfall 上取得了一些成功(例如,不循环遍历数组,仅获取教师[0]).异步模块的文档很全面,但是非常密集,我很难确定我实际需要做什么.我有各种嵌套异步调用的Frankenstein组合,但仍然无法使用.

I've tried getting this working using async and had some success with async.waterfall when doing a proof-of-concept with only getting one of the instructor profiles (e.g. not looping over the array, just getting instructor[0]). The async module's docs are comprehensive, but pretty dense and I'm having a hard time determining what I actually need to do. I had a Frankenstein combination of various nested async calls which still didn't work.

我真的不在乎我如何完成任务-流量控制,承诺,魔术般的粉尘,等等.任何提示都将不胜感激.

I don't really care how I accomplish the task - flow-control, promises, magic pixie dust, whatever. Any hints greatly appreciated.

推荐答案

使用Q来实现承诺,您可能可以执行以下操作:

Using Q for promises, you can probably do something like this:

return Q
.all(course_ids.map(function(course) {
    return HTTP.GET(course); // Assuming this returns a promise
}))
.then(function(course_data) {
    var instructors = [];

    course_data.forEach(function(course) {
        var p = Q
            .all(course.instructor.map(function(instructor) {
                return HTTP.GET(instructor.id);
            }))
            .then(function(instructors) {
                course.instructors_data = instructors;

                return course;
            });

        promises.push(p);
    });

    return Q.all(promises);
});

将使用包含课程的数组进行解析,每个课程均包含在 instructors_data 值中的教师数据数组.

Will resolve with an array containing the courses, each of which contains an array of instructor data in its instructors_data value.

这篇关于nodejs异步:多个依赖的HTTP API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆