javascript - 多次解决 promise

标签 javascript promise rsvp-promise

我正在使用Promises构建一个模块,我在多个 url 上进行多个 http 调用,解析响应,然后再次进行更多 http 调用。

c = new RSVP.Promise({urls:[]}) //Passing a list of urls
c.then(http_module1) // Call the http module
.then(parsing_module) // Parsing the responses and extract the hyperlinks
.then(http_module2) // Making http requests on the data produced by the parser before.
.then(print_module) // Prints out the responses.

问题是 - 如果我使用 promise ,除非发出所有 http 请求,否则我无法解析模块。这是因为 - 一旦 Promise 被解决或拒绝,就无法再次解决或拒绝。

构建我自己的 Promise 版本还是有其他方法?

最佳答案

您可以编写返回 promise 句柄的函数,并创建仍然可链接的可重用部分。例如:

function getPromise(obj){
   return new RSVP.Promise(obj);
}
function callModule(obj){
   return getPromise(obj).then(http_module1);
}

var module = callModule({urls:[]})
  .then(getFoo())
  .then(whatever());

  //etc

关于javascript - 多次解决 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23185477/

相关文章:

javascript - mongodb - 展开嵌套子文档

javascript - $scope 更改后 ng-repeat 不会第二次触发

javascript - 如何在 tensorflow.js 中广播矩阵/向量点积

javascript - PdfMake:函数 getBase64 返回未定义的值

javascript - 定义的函数坚持其未定义的

javascript - 将 promise 包装到 Sync 函数中

Javascript 表单复选框

javascript - 如何使用 catch 和 finally with when.map

javascript - 在将操作传递给 reducer 之前,如何确保所有调用都已返回并推送到数组中?

javascript - Promises/A+ 实现有何不同?