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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在koa发送HTTP响应之前等待url回调?相关的知识,希望对你有一定的参考价值。

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

我的路由器是这样的:

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?

答案
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")
另一答案

好吧首先,这不应该是你的“目标”。 NodeJS works better as ASync

但是,让我们假设您仍然需要它出于某种原因,所以看看sync-request package on npm(那里有一个巨大的注释,你不应该在生产中。

但是,我希望你的意思是如何使这个API更简单(如在一个调用有点东西)。你仍然需要.nextawait,但无论如何它将是一个电话。 如果是这种情况,请评论这个答案我可以写一个我自己使用的方法。

另一答案

这个怎么样 ?

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",
}
});
另一答案

只需将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",
    }; })

我没有测试它,但我认为它会起作用。

以上是关于如何在koa发送HTTP响应之前等待url回调?的主要内容,如果未能解决你的问题,请参考以下文章

如何等待 react native Image Picker 回调函数响应?

如何让任务在 Swift 3 中等待完成

如何等待http请求在处理到angular 7中的下一步之前获得响应[重复]

如何让 Lambda 函数等待异步操作完成?

Camel-netty4:如何在发送下一个请求之前等待响应

C# WebApi 等待 WebHook 响应