简体   繁体   English

Azure Cosmos数据库和功能JavaScript SDK-如何读取异步文件?

[英]Azure Cosmos db and functions javascript sdk - how to read document async?

I have the following code in an apollo resolver: 我在阿波罗解析器中有以下代码:

Mutation: {
    addChannel: (root, args) => {
      client.readDocument("website", {}, (x) => {
        return { id: "Test", name: "Test Channel " + args.name }
      });
    },

where client is a document client: 客户是文档客户:

var DocumentClient = require('documentdb').DocumentClient;

When it's called I'd like to wait on client.readDocument to finish and give me a return value that I will return (the example above is super simple and won't do that) 调用它时,我想等待client.readDocument完成并给我返回的返回值(上面的示例非常简单,不会这样做)

The problem I have is readDocument does something asynchronously in the background but the function isn'ta sync so I can't await it, so the function immediately falls through before the callback has a chance to finish. 我的问题是readDocument在后台异步执行某项操作,但该函数未同步,因此我无法等待,因此该函数在回调有机会完成之前立即掉线。 After the return data is sent (which is undefined) the callback function (x)=>{...} occurs.. 发送返回数据(未定义)后,将发生回调函数(x)=> {...}。

Three solutions I can think of here (which to use will depend on your exact situation): 我在这里可以想到的三种解决方案(要使用哪种解决方案取决于您的实际情况):

  1. v2 of the Cosmos DB API has async/await support so you can use that instead of the callback. Cosmos DB API的v2具有异步/等待支持,因此您可以使用它代替回调。
  2. Wrap the callback code in a promise and await the promise. 将回调代码包装在Promise中,然后等待Promise。
  3. Use a non-async Azure Function and return the result with context.done(result) at the end of the callback. 使用非异步Azure函数,并在回调结束时返回带有context.done(result)context.done(result)

I would suggest you move towards the v2 version of the Javascript API: https://github.com/Azure/azure-cosmos-js 我建议您转向Java API的v2版本: https : //github.com/Azure/azure-cosmos-js

That way you can use the fluent path to the items and use const { body: readDoc } = await item.read(); 这样,您可以使用流畅的items路径,并使用const { body: readDoc } = await item.read(); to read an item async. 读取异步项目。

You can find multiple samples on how this is done here 您可以在此处找到有关如何完成此操作的多个示例

The nature of your code doesn't fit with the callback model that readDocument follows therefore you have no mechanism to pass the document back out from addChannel . 您的代码的性质与readDocument遵循的回调模型不匹配,因此您没有机制将文档从addChannel传递回。

Presuming you don't need to do any pre-processing to the document beforehand, you could use a combination of promises and the async / await syntax eg 假设您不需要事先对文档进行任何预处理,则可以结合使用promises和async / await语法,例如

addChannel: (root, arg) => 
  new Promise((resolve, reject) => 
    client.readDocument("website", {}, (err, x) => 
      err ? reject(err) : resolve(x)
    )
  );
}

Then to "wait" on the document before executing other code 然后在执行其他代码之前先“等待”文档

const doc = await obj.addChannel(...);
// use doc

Note - await can only be used within a function marked as async 注意- await只能在标记为async的函数中使用

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

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