在Azure的Orchestrator的生成器中等待承诺

时间:2019-06-24 21:48:06

标签: javascript node.js azure azure-functions azure-durable-functions

使用Azure中的示例创建持久功能(https://docs.microsoft.com/en-us/azure/azure-functions/durable/quickstart-js-vscode),我有一个工作示例,其中Orchestrator向每个子进程传递了我在代码中手动创建的JSON文档。但是,当我尝试调用从Orchestrator内部返回Promise的函数时,出现错误,如以下代码所示。睡眠函数只是模拟对数据库的调用,因为对Azure Cosmos DB的调用包装在Promise中,并在包含所有文档时返回。

问题-是否有办法让Orchestrator在启动Chaining Pattern之前等待我的睡眠功能解决其Promise问题?

/*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;});

1 个答案:

答案 0 :(得分: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;});