title: How to wait for a url callback before send HTTP response in koa?
body: <p>I have a <code>koa</code> router I need to call a api where will async return result. This means I cannot get my result immediately, the api will call my <code>callback url</code> when it's ok. But now I have to use it like a sync api which means I have to wait until the callback url is called.</p>
  
  <p>My router like this:</p>
  
  <pre><code>router.post("/voice", async (ctx, next) =&gt; {
      // 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",
      }
  })
  </code></pre>
  
  <p>my callback url like this:</p>
  
  <pre><code>router.post("/ret_callback", async (ctx, next) =&gt; {
      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",
      };
  })
  </code></pre>
  
  <p>So how can I make this <code>aync</code> api act like a <code>sync</code> api?</p>