gpt4 book ai didi

node.js - 无法使用 Typescript 3.8.3 版本从 NodeJs 12 中的 Promise.allSettled 获取值

转载 作者:行者123 更新时间:2023-12-03 16:33:22 26 4
gpt4 key购买 nike

我正在使用 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)));
我收到以下编译问题。
enter image description here
我在 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__/*"]
}

最佳答案

你可能想要这样的东西:

  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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com