javascript - Following example of JavaScript Promise giving error
问题描述
I'm creating a promise which will take an input and then return (+3) to it. Then I want to print the result. Why am I getting the error?
var prom = new Promise((item) => item+3);
prom(5).then(console.log(result));
I'm new to Promises. Please help.
解决方案
You don't provide the value to the promise that way.
The only thing you get in your promise is the resolve and reject function, not the value you passed there. It is not a function, it is an object.
You can, instead, wrap it in a function like this:
function prom(item) {
return new Promise((resolve, reject) => resolve(item+3));
}
prom(5).then(result => console.log(result));
推荐阅读
- python - 连接表的自动回滚
- java - Spring Boot可以在jar文件中没有默认application.properties文件的情况下加载吗?
- azure - 使用 terraform apply 时 Azure 函数消失
- python - 无法通过 pipenv 安装 html 或 re
- javascript - 如果组件道具类型无效,如何防止构建应用程序?
- php - 显示预加载器图标 - 直到页面完全加载
- shell - 这是否意味着易受攻击的功能如果我通过程序执行 shell 代码
- java - 使用 Spring Data JpaRepositories 通过 ElementCollections 中的值查询实体
- python - 选择多个标准
- shell - 无法扩展 cURL 命令中使用的变量