繁体   English   中英

一对多的承诺

[英]One-to-many promises

我有一个客户列表,然后每个客户都有一系列要运行的任务。 在psudeocode中:

Get all customers
For each customer, get list of tasks
For each task, run task

但是,我想在运行这些任务时利用JavaScript的异步性质。 有了承诺,我可以做一些事情:

getAllCustomers().then(function(customers) {
  var taskGetters = [];
  customers.forEach(function(customer) {
    taskGetters.push(getCustomerTasks(customer).then(function(tasks) {
      var taskDoers = [];

      tasks.forEach(function(task) {
        taskDoers.push(doTask());
      });

      return promise.all(taskDoers);
    });
  });

  return promise.all(taskGetters);
}).then(function() {
  // clean up
}).catch(function() {
  // handle error
});

然而,这遇到了回调所带来的一些结构性问题 - 我开始嵌套东西而不是保持应有的清晰的psudeo同步结构。 如何在保留承诺的结构优势的同时解决这个问题?

如果我记得你正在使用蓝鸟,那么你可以:

getAllCustomers().map(function(customer) {
  return Promise.map(customer, getCustomerTasks).map(doTask);
}).then(function() {

}).catch(function() {
  // handle error
});

然而,这遇到了回调所带来的一些结构性问题 - 我开始嵌套东西而不是保持应有的清晰的psudeo同步结构。

不。甚至同步等价物也会嵌套:你在那里有两个循环:

customers = getAllCustomers()
for each customer in customers
  tasks = getTasklist()
  for each task in tasks
    run(task)

但是,为了更清晰的代码,我建议使用map而不是forEach手动数组构建:

getAllCustomers()
.then(function(customers) {
  return promise.all(customers.map(function(customer) {
    return getCustomerTasks(customer)
    .then(function(tasks) {
      return promise.all(tasks.map(doTask));
    });
  }));
}).then(function() {
  // clean up
}).catch(function() {
  // handle error
});

一些promise库甚至可能为.then((X)=>all(X.map(…)))模式提供快捷方法。

你将每个回调都变成一个函数,而不是一个闭包,你将拥有一个更好的结构:

function processTasks(tasks){
  var taskDoers = [];

  tasks.forEach(function(task) {
    taskDoers.push(doTask());
  });

  return promise.all(taskDoers);
}

function processCustomer(customer){
    taskGetters.push(getCustomerTasks(customer).then(processTasks));
}

function processCustomers(customers){
    customers.forEach(processCustomer);
}

function cleanup(){

}
function handle(error){

}
getAllCustomers().then(processCustomers).then(cleanup).catch(handle);

您可以根据需要将尽可能多的闭包分成多个函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM