@dapr/durabletask-js

中文文档
1.0.0发布于 2026/03/28MITTypeScript构建来源已验证近 30 天下载 24.6万

A Durable Task Javascript SDK compatible with Dapr Workflow and its underlying Durable Task engine

原始使用文档
文档目录22 节
  1. Supported patterns
  2. Function chaining
  3. Fan-out/fan-in
  4. Human interaction and durable timers
  5. Feature overview
  6. Orchestrations
  7. Activities
  8. Durable timers
  9. Sub-orchestrations
  10. External events
  11. Continue-as-new
  12. Suspend, resume, and terminate
  13. Retry policies (TODO)
  14. Getting Started
  15. Prerequisites
  16. Run the samples
  17. Development
  18. Generating protobufs
  19. Running unit tests
  20. Running E2E tests
  21. Contributing
  22. Trademarks

DurableTask Javascript

License: MIT

This repo contains a Javascript client SDK for use with the Durable Task Framework for Go and Dapr Workflow. With this SDK, you can define, schedule, and manage durable orchestrations using ordinary Javascript code.

This SDK is currently under active development and is not yet ready for production use.

Note that this project is not currently affiliated with the Durable Functions project for Azure Functions.

⚠️ This SDK is currently under active development and is not yet ready for production use. ⚠️

Supported patterns

The following orchestration patterns are currently supported.

Function chaining

An orchestration can chain a sequence of function calls using the following syntax:

function hello(ctx: ActivityContext, name: string): string {
  return `Hello ${name}!`;
}

const sequence: TOrchestrator = async function* (ctx: OrchestrationContext): any {
  const cities = [];

  const result1 = yield ctx.callActivity(hello, "Tokyo");
  cities.push(result1);
  const result2 = yield ctx.callActivity(hello, "Seatle");
  cities.push(result2);
  const result3 = yield ctx.callActivity(hello, "London");
  cities.push(result3);

  return cities;
};

You can find the full sample here.

Fan-out/fan-in

An orchestration can fan-out a dynamic number of function calls in parallel and then fan-in the results using the following syntax:

# activity function for getting the list of work items
function getWorkItems (ctx: ActivityContext): string[] {
    //...
}

function processWorkItem(ctx: ActivityContext): number {
    //...
}

const orchestrator: TOrchestrator = async function* (ctx: OrchestrationContext): any {
    const tasks = [];
    const workItems = yield ctx.callActivity(getWorkItems);
    for (const workItem of workItems) {
    tasks.push(ctx.callActivity(processWorkItem, workItem));
    }
    const results = yield whenAll(tasks);
    return results
}

You can find the full sample here.

Human interaction and durable timers

An orchestration can wait for a user-defined event, such as a human approval event, before proceding to the next step. In addition, the orchestration can create a timer with an arbitrary duration that triggers some alternate action if the external event hasn't been received:

const orchestrator: TOrchestrator = async function* (ctx: OrchestrationContext, order: Order): any {
  if (order.cost < 1000) {
    return "Auto-approvied";
  }

  yield ctx.callActivity(sendApprovalRequest, order);
  const approvalEvent = ctx.waitForExternalEvent("approval_received");
  const timerEvent = ctx.createTimer(10 * 60);
  const winner = yield whenAny([approvalEvent, timerEvent]);
  if (winner == timerEvent) {
    return "Canceled";
  }

  ctx.callActivity(placeOrder, order);
  const approvalDetails = approvalEvent.getResult();
  return `Approved by ${approvalDetails.approver}`;
};

As an aside, you'll also notice that the example orchestration above works with custom business objects. Support for custom business objects includes support for custom classes, custom data classes, and named tuples. Serialization and deserialization of these objects is handled automatically by the SDK.

You can find the full sample here.

Feature overview

The following features are currently supported:

Orchestrations

Orchestrations are implemented using ordinary Python functions that take an OrchestrationContext as their first parameter. The OrchestrationContext provides APIs for starting child orchestrations, scheduling activities, and waiting for external events, among other things. Orchestrations are fault-tolerant and durable, meaning that they can automatically recover from failures and rebuild their local execution state. Orchestrator functions must be deterministic, meaning that they must always produce the same output given the same input.

Activities

Activities are implemented using ordinary Python functions that take an ActivityContext as their first parameter. Activity functions are scheduled by orchestrations and have at-least-once execution guarantees, meaning that they will be executed at least once but may be executed multiple times in the event of a transient failure. Activity functions are where the real "work" of any orchestration is done.

Durable timers

Orchestrations can schedule durable timers using the create_timer API. These timers are durable, meaning that they will survive orchestrator restarts and will fire even if the orchestrator is not actively in memory. Durable timers can be of any duration, from milliseconds to months.

Sub-orchestrations

Orchestrations can start child orchestrations using the call_sub_orchestrator API. Child orchestrations are useful for encapsulating complex logic and for breaking up large orchestrations into smaller, more manageable pieces.

External events

Orchestrations can wait for external events using the wait_for_external_event API. External events are useful for implementing human interaction patterns, such as waiting for a user to approve an order before continuing.

Continue-as-new

Orchestrations can be continued as new using the continue_as_new API. This API allows an orchestration to restart itself from scratch, optionally with a new input.

Suspend, resume, and terminate

Orchestrations can be suspended using the suspend_orchestration client API and will remain suspended until resumed using the resume_orchestration client API. A suspended orchestration will stop processing new events, but will continue to buffer any that happen to arrive until resumed, ensuring that no data is lost. An orchestration can also be terminated using the terminate_orchestration client API. Terminated orchestrations will stop processing new events and will discard any buffered events.

Retry policies (TODO)

Orchestrations can specify retry policies for activities and sub-orchestrations. These policies control how many times and how frequently an activity or sub-orchestration will be retried in the event of a transient error.

Getting Started

Prerequisites

  • Node.js 18 or higher
  • A Durable Task-compatible sidecar, like Dapr Workflow

Run the samples

See the examples directory for a list of sample orchestrations and instructions on how to run them.

Development

Generating protobufs

Protobuf definitions are stored in the ./submodules/durabletask-proto directory, which is a submodule. To update the submodule, run the following command from the project root:

git submodule update --init

Once the submodule is available, the corresponding source code can be regenerated using the following command from the project root:

npm install grpc_tools_node_protoc_ts --save-dev

# generate js codes via grpc-tools
grpc_tools_node_protoc \
--js_out=import_style=commonjs,binary:src/proto \
--grpc_out=grpc_js:src/proto \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` \
-I ./submodules/durabletask-protobuf/protos orchestrator_service.proto

protoc \
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
--ts_out=grpc_js:src/proto \
-I ./submodules/durabletask-protobuf/protos orchestrator_service.proto

Running unit tests

Unit tests can be run using the following command from the project root. Unit tests don't require a sidecar process to be running.

npm run test:unit

Running E2E tests

To run the E2E tests, run the following command from the project root:

npm run test:e2e

This command will start a durabletask sidecar docker container (cgillum/durabletask-sidecar:latest).

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

版本与发布记录

@dapr/durabletask-js 共发布 2 个版本,首次发布于 2024/11/28;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0latest2026/03/287 个月前
0.1.0-alpha.22024/11/282 年前

关于 @dapr/durabletask-js 的常见问题

@dapr/durabletask-js 是什么?

A Durable Task Javascript SDK compatible with Dapr Workflow and its underlying Durable Task engine

如何安装 @dapr/durabletask-js?

在项目目录运行 npm install @dapr/durabletask-js,或使用 pnpm add / yarn add / bun add。

@dapr/durabletask-js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dapr/durabletask-js 可以商用吗?

@dapr/durabletask-js 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dapr/durabletask-js 的下载量有多少?

根据 npm 官方下载统计,@dapr/durabletask-js 在 2026/10/12 – 2026/11/10 这 30 天内被下载 245,612 次,日均约 8,187 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dapr/durabletask-js 还在维护吗?

最新版本 1.0.0 发布于 2026/03/28(7 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/11/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。