0

使用 Azure 中的示例创建持久函数 ( https://docs.microsoft.com/en-us/azure/azure-functions/durable/quickstart-js-vscode ),我有一个 Orchestrator 的工作示例通过每个子进程我在代码中手动创建的 JSON 文档。但是,当我尝试从 Orchestrator 中调用一个返回 Promise 的函数时,我收到一个错误,如下面的代码所示。sleep 函数只是对 DB 调用的模拟,因为对 Azure Cosmos DB 的调用被包装在 Promise 中,并在它拥有所有文档时返回。

问题——有没有办法让 Orchestrator 在开始链接模式之前等待我的睡眠功能来解决它​​的承诺?

/*WORKING EXAMPLE*/
const df = require('durable-functions');

module.exports = df.orchestrator(function*(context){
context.log("Starting chain sample");

let output = [];

context.log("MERGED DOC--START:");

let mergedDoc = {/*SOME MANUALLY CREATED JSON GOES HERE*/};

context.log("MERGED DOC:--EMD");
//let mergedDoc = {};
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-1","data":mergedDoc}));
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-2","data":mergedDoc}));
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-2","data":mergedDoc}));


return output;
});

+++++++++++++++++++++++++++++++

/*FAILING EXAMPLE*/
const df = require('durable-functions');
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = df.orchestrator(function*(context){
context.log("Starting chain sample");

let output = [];
await sleep(5000);
let mergedDoc = {};
/*
ERROR FROM CONSOLE
await sleep(5000);
^^^^

SyntaxError: Unexpected identifier
  at createScript (vm.js:80:10)
  at Object.runInThisContext (vm.js:139:10)
  at Module._compile (module.js:607:28)
*/
context.log("MERGED DOC:" + mergedDoc);
//let mergedDoc = {};
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-1","data":mergedDoc}));
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-2","data":mergedDoc}));
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-3","data":mergedDoc}));4
return output;});
4

1 回答 1

0

解析度:

const df = require('durable-functions');

module.exports = df.orchestrator(function*(context){
context.log("Starting chain sample");

let output = [];
let mergedDoc = {};

/*
Add code into the E1_SayHello function for STEP-0 to perform the asych call and return the results
back to the Orchestrator.  Since this is the Chaining Pattern STEP-1 won't execute until STEP-0 has
completed.  The result is that my E1_SayHello code has a list of if/else statements looking at 
what STEP-## is passed into it.  From there it runs different functions.
*/
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-0","data":mergedDoc}));
mergedDoc = ouput[0]
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-1","data":mergedDoc}));
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-2","data":mergedDoc}));
output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-3","data":mergedDoc}));
return output;});

于 2019-06-25T17:12:13.327 回答