问题
With node v10.15.1 I try to use Promise.allSettled()
to executes batches of Promise but it throw me an error
TypeError: Promise.allSettled is not a function
Is Promise.all()
returning a promise ?
The Main
function below return an object.
Other functions use some Promises to create its "sub-object".
Why I need "batch of promise" : To create a sub-object, all the required promises must be settled. But not all the sub-objects are needed in the "main object".
const path = require('path');
const os = require('os');
const si = require('systeminformation');
function getFoo() {
// all these promises have to be settled to construct the sub-object
return Promise.all([si.system(), si.chassis()]).then(([system, chassis]) => {
return { /* hidden for brevity : use system and chassis to return a single object */ };
})
.catch(ex => { /* hidden for brevity */ });
}
function getBar() {
// all these promises have to be settled to construct the sub-object
return Promise.all([si.osInfo(), si.uuid()]).then(([osInfo, uuid]) => {
return { /* hidden for brevity : use osInfo and uuid to return a single object */ };
})
.catch(ex => { /* hidden for brevity */ });
}
function getBaz() {
// all these promises have to be settled to construct the sub-object
return Promise.all([os.networkInterfaces(), si.networkInterfaceDefault()]).then(([interfaces, defaultInterface]) => {
return { /* hidden for brevity : use interfaces and defaultInterface to return a single object */ };
})
.catch(ex => { /* hidden for brevity */ });
}
function Main() {
// some of these promises can be rejected
Promise.allSettled([ getFoo(), getBar(), getBaz() ])
.then(([foo, bar, baz]) => {
return { foo, bar, baz }
})
.catch(ex => { /* hidden for brevity */ });
}
Main();
One Example of expected object
{
foo: {
prop: 'example',
someOtherProps: 'We are there!'
},
baz: {
test: 50
}
}
回答1:
Promise.allSettled is available in node version >= 12.9
回答2:
Promise.allSettled is not yet available for Node environments. As a workaround until support is added, you can use the npm package: es-shims/Promise.allSettled.
回答3:
Not yet. At the time of this post, allSettled
is on stage 4.
There are plan to add allSettled
to Promise into typescript and polyfilling is already available using babel and core-js@3. Or you could use one of the many userland implementation available. It may take some time before it get to nodejs nativity, but that doesn't stop you from using it anyway right now.
回答4:
allSettled = function(promiseList) {
let results = new Array(promiseList.length);
return new Promise((ok, rej) => {
let fillAndCheck = function(i) {
return function(ret) {
results[i] = ret;
for(let j = 0; j < results.length; j++) {
if (results[j] == null) return;
}
ok(results);
}
};
for(let i=0;i<promiseList.length;i++) {
promiseList[i].then(fillAndCheck(i), fillAndCheck(i));
}
});
}
回答5:
afaik, Promise.allSettled()
is not available yet.
You can still use Promise.all(), it is indeed returning a promise that resolves when ALL promises inside the array resolves, or rejects when one of them rejects.
来源:https://stackoverflow.com/questions/57576249/execute-batch-of-promise-with-promise-allsettled