【问题标题】:Waiting for Promise inside Azure's Orchestrator's Generator在 Azure 的 Orchestrator 生成器中等待 Promise
【发布时间】:2019-06-24 21:48:06
【问题描述】:

使用 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 等待 my sleep 函数来解决它的 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;});

【问题讨论】:

  • 看起来不可能。要休眠,您需要处于异步函数中。所以回调或承诺。发电机是同步的。有一些方法可以将生成器包装在 Promise 中。除非 df.orchestrator() 方法也接受承诺,否则我认为在你的情况下这是不可能的。
  • 您可以将 df.orchestrator 方法包​​装在异步函数中并在调用 df.orchestrator() 之前休眠
  • 感谢您的建议。我通过添加一个新的“STEP”Step-0 找到了完成此任务的方法,该步骤的工作是执行异步功能。事实证明,我可以修改“E1-SayHello”代码的结果以返回数据,这样在完成“Step-0”后,我只需将mergedDoc 设置为输出数组中的第一个值。 output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEO-0","data":{}})); mergedDoc = output[0]; output.push(yield context.df.callActivity("E1_SayHello", {"step":"STEP-1","data":mergedDoc}));

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


【解决方案1】:

分辨率:

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-02-14
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多