使用 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;});