Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: undefined is not a promise

I got error of Uncaught TypeError: undefined is not a promise

const p = Promise((resolve, reject) => {   resolve('ok') })  p.then(resp => console.log(resp)) 

https://jsbin.com/daluquxira/edit?js,console,output

what's wrong with above code?

like image 540
esther Joo Avatar asked Nov 24 '17 08:11

esther Joo


People also ask

How do you resolve a Promise?

Promise resolve() method: Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

What is uncaught in Promise?

What does that log "Uncaught (in promise)" mean? It means that there was an error in one of our promises, but we did not write any code in order to handle that error and try to catch it.

What is the reason for getting error TypeError undefined is not an object?

This is a common JavaScript error that happens when you try to call a function before it is defined. You get this error when you try to execute a function that is uninitialized or improperly initialized . It means that the expression did not return a function object.


1 Answers

You need to instantiate the Promise.

In this case:

const p = new Promise((resolve, reject) => {    resolve('ok')  })    p.then(resp => console.log(resp))
like image 163
mersocarlin Avatar answered Oct 14 '22 10:10

mersocarlin