一旦所有内部并发承诺都已解决或拒绝,解决承诺
Resolve a promise once all internal concurrent promises have resolved or rejected
我正在寻找类似于 Promise.all
的东西,即使在一个或多个 promise 拒绝或抛出错误的情况下,它也会继续同时解决 promise。每个请求不依赖于另一个请求。
接近我想要的-请看评论
function fetchRequest (request) {
return new Promise(function (resolve, reject) {
fetch(request)
.then(function(response) {
return response.text();
}).then(function (responseXML) {
//Do something here. Maybe add data to dom
resolve(responseXML);
}).catch(function (err) {
reject(new Error(err));
}
}
function promiseRequests (requests) {
var result = Promise.resolve();
for (var i = 0; i < requests.length; i++) {
result = fetchRequest(requests[i])
}
//This is wrong as it will resolve when the last promise in the requests array resolves
// - not when all requests resolve
resolve(result);
}
promiseRequests(['url1.com', 'url2.com']).then(function (data) {
console.log('All requests finished');
//optionally have data be an array of resolved and rejected promises
});
我成功地使用了 Promise.all
并且只解析了 fetchRequest 承诺,这导致了预期的结果(结果数组和 undefined
's),但我觉得这是错误的做事方式。它还消除了我使用抛出错误的能力。
有效,但感觉像是 resolve 的不正确使用
function fetchRequest (request) {
return new Promise(function (resolve, reject) {
fetch(request)
.then(function(response) {
return response.text();
}).then(function (responseXML) {
resolve(responseXML);
}).catch(function (err) {
resolve();
}
}
Promise.all([fetchRequest('url1.com'), fetchRequest('url2.com')]).then(function (data) {
console.log('All requests finished', data); //data could be ['resultXML', undefined]
});
请仅原生 es6 promise API 回答谢谢。
你实质上是在寻求一种方法来吞下任何错误。因此,像这样的功能将是您最好的选择:
function swallow(p) {
// transforms rejected promises into promises fulfilled with undefined
return p.catch(function () { });
}
您将按如下方式使用它:
Promise.all([swallow(fetch('url1.com')), swallow(fetch('url2.com'))]).then(function (data) {
console.log('All requests finished', data); //data could be ['resultXML', undefined]
});
甚至
const promises = ['url1.com', 'url2.com'].map(fetch).map(swallow);
Promise.all(promises).then(function (data) {
// ...
});
I have succeeded in using Promise.all
together with only ever resolving the fetchRequest
promises
基本上就是这样。 ES6 没有像 allSettled
(Q) or settle
(Bluebird 2.x) for this case, so we will need to use Promise.all
similar to like you did. Bluebird even has a dedicated .reflect()
实用程序这样的辅助函数。
然而,如果被拒绝,您不会使用 undefined
解决它们,而是使用一些有用的值来识别错误。
function promiseRequests(requests) {
return Promise.all(requests.map(request => {
return fetch(request).then(res => {
return {value:res};
}, err => {
return {reason:err};
});
}));
}
我正在寻找类似于 Promise.all
的东西,即使在一个或多个 promise 拒绝或抛出错误的情况下,它也会继续同时解决 promise。每个请求不依赖于另一个请求。
接近我想要的-请看评论
function fetchRequest (request) {
return new Promise(function (resolve, reject) {
fetch(request)
.then(function(response) {
return response.text();
}).then(function (responseXML) {
//Do something here. Maybe add data to dom
resolve(responseXML);
}).catch(function (err) {
reject(new Error(err));
}
}
function promiseRequests (requests) {
var result = Promise.resolve();
for (var i = 0; i < requests.length; i++) {
result = fetchRequest(requests[i])
}
//This is wrong as it will resolve when the last promise in the requests array resolves
// - not when all requests resolve
resolve(result);
}
promiseRequests(['url1.com', 'url2.com']).then(function (data) {
console.log('All requests finished');
//optionally have data be an array of resolved and rejected promises
});
我成功地使用了 Promise.all
并且只解析了 fetchRequest 承诺,这导致了预期的结果(结果数组和 undefined
's),但我觉得这是错误的做事方式。它还消除了我使用抛出错误的能力。
有效,但感觉像是 resolve 的不正确使用
function fetchRequest (request) {
return new Promise(function (resolve, reject) {
fetch(request)
.then(function(response) {
return response.text();
}).then(function (responseXML) {
resolve(responseXML);
}).catch(function (err) {
resolve();
}
}
Promise.all([fetchRequest('url1.com'), fetchRequest('url2.com')]).then(function (data) {
console.log('All requests finished', data); //data could be ['resultXML', undefined]
});
请仅原生 es6 promise API 回答谢谢。
你实质上是在寻求一种方法来吞下任何错误。因此,像这样的功能将是您最好的选择:
function swallow(p) {
// transforms rejected promises into promises fulfilled with undefined
return p.catch(function () { });
}
您将按如下方式使用它:
Promise.all([swallow(fetch('url1.com')), swallow(fetch('url2.com'))]).then(function (data) {
console.log('All requests finished', data); //data could be ['resultXML', undefined]
});
甚至
const promises = ['url1.com', 'url2.com'].map(fetch).map(swallow);
Promise.all(promises).then(function (data) {
// ...
});
I have succeeded in using
Promise.all
together with only ever resolving thefetchRequest
promises
基本上就是这样。 ES6 没有像 allSettled
(Q) or settle
(Bluebird 2.x) for this case, so we will need to use Promise.all
similar to like you did. Bluebird even has a dedicated .reflect()
实用程序这样的辅助函数。
然而,如果被拒绝,您不会使用 undefined
解决它们,而是使用一些有用的值来识别错误。
function promiseRequests(requests) {
return Promise.all(requests.map(request => {
return fetch(request).then(res => {
return {value:res};
}, err => {
return {reason:err};
});
}));
}