【发布时间】:2021-03-17 14:48:25
【问题描述】:
如果我想使用 Promise 来确保函数首先运行但不关心返回值是什么,我该如何解决它? resolve(true) 是最佳实践吗?
let getShoes = return new Promise((resolve, reject) => {
// do something
//resolve(true); resolve using true?
});
getShoes.then(() => {
// do something else but don't need the return from getShoes...only need to ensure it is run first
})
【问题讨论】:
-
不需要
return new Promise...只需return fetch。 fetch 是基于 promise 的。 -
真 @David784 。如果它不使用 fetch 而只是先进行计算而我不关心返回但只是想确保它首先运行呢?
-
我编辑了这个问题,使其无需 API 调用
-
我觉得undefined比较好,在异步函数中undefined是默认返回值,和普通函数一样
-
已经有一段时间了,(也许是节点 8?)但是我曾经有一个 node.js 项目,我尝试只调用
resolve()没有值(未定义),奇怪的事情发生了。当我添加一个返回值时,奇怪的事情就停止了。可能只是一个错误,也许它不再是一个问题。但从那时起,我总是确保在解决和拒绝中具有一定的价值,即使它只是一个布尔值或整数或其他什么。只是我的 0.02 美元
标签: javascript