本文介绍了无法从TypeJ 3.8.3版本的NodeJs 12中从Promise.allSettled获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Promise.allSettled()
函数及其用法来学习NodeJs 12.我写了下面的代码.我可以在控制台中打印状态,但由于存在编译问题,因此无法打印该值.
I am learning NodeJs 12 with Promise.allSettled()
function and its usage.I have written the following code. I am able to print the status in the console but unable to print the value as it is giving compilation issue.
const p1 = Promise.resolve(50);
const p2 = new Promise((resolve, reject) =>
setTimeout(reject, 100, 'geek'));
const prm = [p1, p2];
Promise.allSettled(prm).
then((results) => results.forEach((result) =>
console.log(result.status,result.value)));
我遇到以下编译问题.
I am getting the following compilation issue.
我在tsconfig.json下面提供.
I provide below the tsconfig.json.
{
"compilerOptions": {
"target": "es2017",
"lib": ["es6","esnext", "dom"],
"allowJs": true,
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"outDir": "./lib",
"strict": true,
"esModuleInterop": true,
"typeRoots": [ "./types", "./node_modules/@types"]
},
"include": ["src"],
"exclude": ["**/__tests__/*"]
}
推荐答案
您可能想要这样的东西:
You might want something like this:
Promise.allSettled(prm).
then((results) => results.forEach(result => {
if (result.status === 'fulfilled') {
console.log(result.status,result.value);
} else {
console.log(result.status,result.reason);
}
});
value
仅在满足状态时存在,但不包括其中一个承诺有错误的情况.
value
only exists if the status is fulfilled, but it doesn't cover cases where one of the promises had an error.
这篇关于无法从TypeJ 3.8.3版本的NodeJs 12中从Promise.allSettled获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!