我正在使用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/