简体   繁体   中英

@azure/cosmos.js - get database synchronously

I'm using node.js SDK - @azure/cosmos.js to retrieve a database synchronously. The documentation on usage of SDK( https://www.npmjs.com/package/@azure/cosmos/v/2.0.0-3 ) -

const database = await client.database(databaseId);

await can only be used in async methods. How can I retrieve database in synchronous method?

Azure Cosmos SDK dev lead here.

There are no sync APIs in @azure/cosmos. @azure/cosmos relies on apis that are async only such as https.request. While there are libraries that can farm those APIs out to a child worker while blocking, they are not commonly used and we don't have plans to support it.

As James recommended, take a look at using the await syntax which makes it read like sync code without fighting the way Node.js wants to do I/O work.

async function main() {
    const db = await client.databases.create("foo");
}

main().catch(console.error);

You can also look at the Python SDK which has a sync API (for now 😜).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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