@adent/promises 中文文档教程

发布于 5年前 浏览 7 项目主页 更新于 3年前

Usage

const tasks = [
  task1, task2, task3, ...
];

它们都必须是 fn(resolve,reject),即“可承诺的功能”。

Methods

promises.parallel(tasks)

一起运行所有任务并返回 promise,它在所有任务都解决的那一刻就完成了。 Result 是所有结果的数组

promises.serial(tasks)

按给定顺序运行所有任务。 解决上一个任务时调用下一个任务。 返回承诺,在解决最后一个任务时完成。 Result 是所有结果的数组

promises.allSettled(tasks)

等到所有任务都解决了(每个任务都可能解决或拒绝)。 返回一个在所有给定任务都已解决或被拒绝后解决的承诺,其中每个对象都描述每个承诺的结果。

promises.race(tasks)

等到任何任务被解决或拒绝。 如果返回的任务已解决,则使用已解决的可迭代对象中的第一个任务的值来解决。 如果拒绝,则拒绝的原因来自第一个被拒绝的任务。

Usage

const tasks = [
  task1, task2, task3, ...
];

All of them have to be fn(resolve,reject), i.e. "promisable function".

Methods

promises.parallel(tasks)

Run all tasks together and returns promise, which is fullfilled in the moment the all tasks are resolved. Result is the array of all results

promises.serial(tasks)

Run all tasks in the given order. Next task is invoked when previous is resolved. Returns promise, which is fullfilled in the moment the last tasks are resolved. Result is the array of all results

promises.allSettled(tasks)

Wait until all tasks have settled (each may resolve or reject). Returns a promise that resolves after all of the given tasks have either resolved or rejected, with an array of objects that each describe the outcome of each promise.

promises.race(tasks)

Wait until any of the task is resolved or rejected. If the returned task resolves, it is resolved with the value of the first task in the iterable that resolved. If it rejects, it is rejected with the reason from the first task that was rejected.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文