简体   繁体   English

架构嵌套 API 异步调用

[英]Architecture Nested API async calls

In order to use some online service, I need to call an API three times.为了使用一些在线服务,我需要调用一个 API 3 次。

  1. Upload the file上传文件
  2. Request an operation请求操作
  3. Download the file下载文件

I can handle all operations individually but I have hard times to sequence them as they are asynchronous.我可以单独处理所有操作,但我很难对它们进行排序,因为它们是异步的。 I wish I can handle all operations in one function call and all I am asking here is some advice to start on the right foot.我希望我可以在一个函数调用中处理所有操作,我在这里问的只是一些建议,让他们从正确的角度开始。

I have been playing with promises but got lost in the progress.我一直在玩承诺,但在进展中迷失了方向。

 function main(){ //Pseudo code calling async step1 exit if step1 failed calling async step2 exit if step2 failed calling async ste3 exit if step3 failed return OK }

Thanks in advance.提前致谢。

Since you've given us no real code and no specific information on your APIs, I will offer an answer assuming that the APIs return promises or can be made to return promises.由于您没有向我们提供真正的代码,也没有关于您的 API 的具体信息,我将假设 API 返回承诺或可以返回承诺来提供答案。 In that case, sequencing and doing error handling is quite simple:在这种情况下,排序和错误处理非常简单:

ascync function main() {
     let step1Result = await step1();
     let step2Result = await step2();
     let step3Result = await step3();
     return finalValue;
}

// usage
main().then(finalResult => {
    console.log(finalResult);
}).catch(err => {
    console.log(err);
});

Some things to know:需要知道的一些事情:

  1. Because main is declared async , it can use await internal to its implementation.因为main被声明为async ,它可以在其实现内部使用await
  2. Because main is delcared async , it will always return a promise因为main被声明为async ,所以它总是会返回一个 promise
  3. This code assumes that step1() , step2() and step3() return promises that are linked to the completion or error of one or more asynchronous operations.此代码假定step1()step2()step3()返回与一个或多个异步操作的完成或错误相关的承诺。
  4. When the function is executing, as soon as it hits the first await , the function will return an unresolved promise back to the caller.当函数正在执行时,一旦它遇到第一个await ,该函数就会向调用者返回一个未解决的承诺。
  5. If any of the await stepX() calls reject (eg have an error), that will abort further execution of the function and reject the promise that was previously returned with that specific error.如果任何await stepX()调用拒绝(例如有错误),这将中止函数的进一步执行并拒绝先前返回的带有该特定错误的承诺。 It works analogous to throw err in synchronous programming, but the throw is caught automatically by the async function and turned into a reject for the promise that it already returned.它的工作原理类似于同步编程中的throw err ,但是 throw 会被async函数自动捕获并变成对它已经返回的承诺的拒绝。
  6. Your final return value in the function is not actually what is returned because remember, a promise was already returned from the function when it hit the first await .您在函数中的最终返回值实际上并不是返回的值,因为请记住,当函数遇到第一个await时,已经从该函数返回了一个承诺。 Instead, the value you return becomes the resolved value of that promise that was already returned.相反,您返回的值成为已返回的承诺的已解析值。
  7. If your API interfaces don't currently return promises, then you can usually find a wrapper that someone has done to create a version that will return promises (since promises are the modern way of working with asynchronous interfaces in Javascript).如果您的 API 接口当前不返回承诺,那么您通常可以找到某人所做的包装器来创建将返回承诺的版本(因为承诺是在 Javascript 中使用异步接口的现代方式)。 Or, you can create your own wrapper.或者,您可以创建自己的包装器。 In node.js, you can use util.promisify() or you can manually make your own wrapper (just takes a few lines of code).在 node.js 中,您可以使用util.promisify()或者您可以手动制作自己的包装器(只需几行代码)。
  8. If you need to catch an error within the function in order to handle it and continue, you can either wrap a given await call in try/catch or use .catch() on the promise and "handle" the error and continue processing, again similar to handling exceptions in synchronous code.如果您需要在函数中捕获错误以处理它并继续,您可以在try/catch包装给定的await调用或在承诺上使用.catch()并“处理”错误并继续处理,再次类似于处理同步代码中的异常。

If any of the steps had an error and rejected their promise, then main() will abort and reject similar to throwing an exception in synchronous code.如果任何步骤有错误并拒绝了他们的承诺,那么main()将中止并拒绝,类似于在同步代码中抛出异常。

See also:也可以看看:

How to chain and share prior results with Promises 如何使用 Promise 链接和共享先前的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM