作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Promise.allSettled()
学习 NodeJs 12功能及其用法。
我已经编写了以下代码。我能够在控制台中打印状态,但无法打印值,因为它给出了编译问题。
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)));
我收到以下编译问题。
{
"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__/*"]
}
最佳答案
你可能想要这样的东西:
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
仅当状态已完成时才存在,但不包括其中一个 promise 有错误的情况。
关于node.js - 无法使用 Typescript 3.8.3 版本从 NodeJs 12 中的 Promise.allSettled 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63119998/
我是一名优秀的程序员,十分优秀!