我使用Bluebird 3启用取消。取消在以下用例中使用的工具:
var resourcesPromise = Promise.map(resourceIds, function(id) {
return loadResource(id);
});
resourcesPromise.catch(function() {
resourcesPromise.cancel();
});`
如果其中一个资源无法加载,则resourcesPromise将被拒绝,我想停止加载所有其他资源。但据我所知,取消资源促销并不起作用,因为它已被拒绝。
编辑:我目前正在考虑以下变体:
var resourcesPromise = new Promise(function(resolve, reject) {
var intermediatePromise = Promise.map(resourceIds, function(id) {
return loadResource(id).catch(function(error) {
intermediatePromise.cancel();
reject(error);
});
}).then(resolve, reject);
});
(我可能已经找到合法使用" .then(解决,拒绝)"反模式!)
任何想法为什么Promise.map都没有这样的工作?
答案 0 :(得分:0)
With map you are parallelizing the resource loading, maybe is better to use Promise.each to laoad resources in sequence. In such case you don't need to cancel the promise to stop loading the remaining resources when one fails.
RSpec.describe ApplicationHelper, :type => :helper do
helper do
def user_signed_in?
user.present?
end
end
describe "#session_button" do
context "user signed in" do
let(:user) { FactoryGirl.create :user }
it "returns a link to Restricted Area" do
expect(session_button).to include "Área Restrita"
expect(session_button).to include user_index_path
end
end
context "user not signed in" do
let(:user) { nil }
it "returns a link to New Session" do
expect(session_button).to include "Login"
expect(session_button).to include new_user_session_path
end
end
end
end
Another option would be to pass to the map function an option object like in which you can specify the concurrency limit.
var resourcesPromise = Promise.map(resourceIds, function(id) {
return loadResource(id);
});
resourcesPromise.catch(function() {
resourcesPromise.cancel();
});`