如何在koa发送HTTP响应之前等待url回调?

时间:2018-06-06 16:06:39

标签: node.js asynchronous events synchronization koa

我有一个koa路由器,我需要调用api,异步返回结果。这意味着我无法立即得到我的结果,api会在我的callback url好的时候调用它。但现在我必须像使用sync api一样使用它,这意味着我必须等到调用回调网址。

我的路由器是这样的:

router.post("/voice", async (ctx, next) => {
    // call a API here
    const params = {
        data: "xxx",
        callback_url: "http//myhost/ret_callback",
    };
    const req = new Request("http://xxx/api", {
        method: "POST",
        body: JSON.stringify(params),
    });
    const resp = await fetch(req);
    const data = await resp.json();

    // data here is not the result I want, this api just return a task id, this api will call my url back
    const taskid = data.taskid;

    // now I want to wait here until I got "ret_callback"

    // .... wait .... wait
    // "ret_callback" is called now
    // get the answer in "ret_callback"
    ctx.body = {
        result: "ret_callback result here",
    }
})

我的回调网址是这样的:

router.post("/ret_callback", async (ctx, next) => {
    const params = ctx.request.body;

    // taskid will tell me this answer to which question
    const taskid = params.taskid;
    // this is exactly what I want
    const result = params.text;

    ctx.body = {
        code: 0,
        message: "success",
    };
})

那么我怎样才能使aync api像sync api一样行事?

4 个答案:

答案 0 :(得分:0)

function getMovieTitles(substr) {
        let movies = [];
        let fdata = (page, search, totalPage) => {
            let mpath = {
                host: "jsonmock.hackerrank.com",
                path: "/api/movies/search/?Title=" + search + "&page=" + page,
            };
            let raw = '';
            https.get(mpath, (res) => {
                res.on("data", (chunk) => {
                    raw += chunk;
                });

                res.on("end", () => {
                    tdata = JSON.parse(raw);
                    t = tdata;
                    totalPage(t);
                });
            });
        }

    fdata(1, substr, (t) => {
        i = 1;
        mdata = [];
        for (i = 1; i <= parseInt(t.total_pages); i++) {
                fdata(i, substr, (t) => {
                    t.data.forEach((v, index, arrs) => {
                        movies.push(v.Title);
                        if (index === arrs.length - 1) {
                            movies.sort();
                            if (parseInt(t.page) === parseInt(t.total_pages)) {
                                movies.forEach(v => {
                                    console.log(v)
                                })
                            }
                        }
                    });
                });
            }
        });


}

getMovieTitles("tom")

答案 1 :(得分:0)

首先,这对您来说不是目标。 NodeJS works better as ASync

但是,让我们假设由于某些原因您仍然想要它,因此请看一下sync-request package on npm(那里有一个很大的提示,您不应在生产中使用它。

但是,我希望您的意思是如何使该API更简单(就像在一次调用中那样)。您仍然需要.nextawait,但无论如何,这将是一个电话。
如果是这种情况,请对此答案发表评论。我可以给你写一个我可能使用的方法。

答案 2 :(得分:0)

这个怎么样?

router.post("/voice", async (ctx, next) => {

const params = {
    data: "xxx",
    callback_url: "http//myhost/ret_callback",
};
const req = new Request("http://xxx/api", {
    method: "POST",
    body: JSON.stringify(params),
});
const resp = await fetch(req);
const data = await resp.json();

// data here is not the result I want, this api just return a task id, this api will call my url back
const taskid = data.taskid;

let response = null;
try{

  response = await new Promise((resolve,reject)=>{
      //call your ret_callback and when it finish call resolve(with response) and if it fails, just reject(with error);
  });
}catch(err){
  //errors
}

// get the answer in "ret_callback"
ctx.body = {
    result: "ret_callback result here",
}
});

答案 3 :(得分:0)

只需将resolve()传递给另一个函数。例如,您可以这样操作:

// use a map to save a lot of resolve()
const taskMap = new Map();

router.post("/voice", async (ctx, next) => {
    // call a API here
    const params = {
        data: "xxx",
        callback_url: "http//myhost/ret_callback",
    };
    const req = new Request("http://xxx/api", {
        method: "POST",
        body: JSON.stringify(params),
    });
    const resp = await fetch(req);
    const data = await resp.json();

    const result = await waitForCallback(data.taskid);

    ctx.body = {
        result,
    } })

const waitForCallback = (taskId) => {
    return new Promise((resolve, reject) => {
        const task = {};
        task.id = taskId;
        task.onComplete = (data) => {
            resolve(data);
        };
        task.onError = () => {
            reject();
        };
        taskMap.set(task.id, task);
    });
};

router.post("/ret_callback", async (ctx, next) => {
    const params = ctx.request.body;

    // taskid will tell me this answer to which question
    const taskid = params.taskid;
    // this is exactly what I want
    const result = params.text;

    // here you continue the waiting response
    taskMap.get(taskid).onComplete(result);
    // not forget to clean rubbish
    taskMap.delete(taskid);

    ctx.body = {
        code: 0,
        message: "success",
    }; })

我没有测试它,但是我认为它可以工作。