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

问题描述 投票:4回答:4

我有一个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?

node.js asynchronous events synchronization koa
4个回答
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")

0
投票

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

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

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


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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.