首页 > 解决方案 > 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.

标签: javascriptecmascript-6es6-promise

解决方案


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));


推荐阅读