then / promise

Bare bones Promises/A+ implementation
https://www.promisejs.org
MIT License
2.58k stars 312 forks source link

Promise do not change to "resolve" mode when calling the function again. #141

Closed roysG closed 7 years ago

roysG commented 7 years ago

I call to "roy" function and then i call again to the same function, but in the second time i call to my resolve, but resolve it not back and instead it stuck on "pending" state.

roy = (num=0)=>{
return new Promise((resolve,reject)=>{
if(!num)
    roy(num+1);
else
resolve(55);

})
}
roy().then(x=>{
console.log('x:',x);
})

Console:

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined} proto : Promise [[PromiseStatus]] : "pending" [[PromiseValue]] : undefined

constgen commented 7 years ago

It works as expected. Every call of roy() gives a new instance of a Promise. So on the first call it returns a new Promise that will never be resolved because condition will never call else statement. num is a local variable. It is not persisted anywhere and destroyed after roy() is called. A Promise will be resolved only in case when you pass a positive number to roy(number). There is defiantly an issue in the logic.

What is your expected result? If you want this to be resolved

roy().then(x=>{
   console.log('x:',x);
})

then remove this

if(!num)
   roy(num+1);
else
roysG commented 7 years ago

Hi @constgen, I just give an example for load more items. Lets say example for load more likes of instagram post, till you get end.

So in this case you will need to call again to same function but with another next_token.

The problem, when you will finish iterate all likes and then go out to your resolve you will find yourself stuck in pending mode.

What i am trying to figure out, how can i go to .then function, when i need to call again to the same function.

Hopefully i succeed to explain myself better.

roysG commented 7 years ago

For those who have also problem like me, i solved the problem with using create new function with name: "loadMore", inside the promise. In the "loadMore" i call again the request i need.

constgen commented 7 years ago

Any way it was not an issue of promises themselves. It is recommended to ask such questions in forums, Slack channels, etc.