CosmosDB stored procedures - promise instead of callback

女生的网名这么多〃 提交于 2019-12-22 06:51:14

问题


Is it possible to use javascript promises instead of regular callbacks within CosmosDB (DocumentDB) stored procedure API calls? An usage would be implementing pagination.

For example

token = getToken();
doSomething(token);    

//....
function getToken(....) {   

  //...
  collection.queryDocuments(link, query, queryOptions, function(error, documents, responseOptions) {
    return responseOptions.continuation;
  });
}

would not work because the token is returned within a callback, and the execution continues. Could you please give an example of how you would implement this?


回答1:


The version of ECMAScript referenced in Cosmos DB docs supports async/await and Promises. I am able to use both of those in my stored procedures.

Here's a function that returns a promise that makes a parameterized document query:

function queryDocumentsAsync(sql, parameters, options) {
    const querySpec = {
        query: sql,
        parameters: parameters
    };

    return new Promise((resolve, reject)=>{
        let isAccepted = __.queryDocuments(__.getSelfLink(), querySpec, options || {}, (err, feed, options) => {
            if(err) reject(err);
            resolve({
                feed: feed,
                options: options
            });
        });
        if(!isAccepted) throw "Query was not accepted.";
    });
}

I am seeing some limitations around forcing a rollback with this approach, though. If you throw an Error, it gets swallowed by the promise chain and never gets out.




回答2:


Here's an example on how to use async await for query and replace scenario.

function async_sample() {
    const ERROR_CODE = {
        NotAccepted: 429
    };

    const asyncHelper = {
        queryDocuments(sqlQuery, options) {
            return new Promise((resolve, reject) => {
                const isAccepted = __.queryDocuments(__.getSelfLink(), sqlQuery, options, (err, feed, options) => {
                    if (err) reject(err);
                    resolve({ feed, options });
                });
                if (!isAccepted) reject(new Error(ERROR_CODE.NotAccepted, "replaceDocument was not accepted."));
            });
        },

        replaceDocument(doc) {
            return new Promise((resolve, reject) => {
                const isAccepted = __.replaceDocument(doc._self, doc, (err, result, options) => {
                    if (err) reject(err);
                    resolve({ result, options });
                });
                if (!isAccepted) reject(new Error(ERROR_CODE.NotAccepted, "replaceDocument was not accepted."));
            });
        }
    };

    async function main() {
        let continuation;
        do {
            let { feed, options } = await asyncHelper.queryDocuments("SELECT * from c", { continuation });

            for (let doc of feed) {
                doc.newProp = 1;
                await asyncHelper.replaceDocument(doc);
            }

            continuation = options.continuation;
        } while (continuation);
    }

    main().catch(err => getContext().abort(err));
}


来源:https://stackoverflow.com/questions/46910406/cosmosdb-stored-procedures-promise-instead-of-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!