Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
roysG opened this issue Nov 4, 2017 · 4 comments
Closed

Comments

@roysG
Copy link

roysG commented Nov 4, 2017

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

@roysG roysG changed the title Promise do not change to "resolve" mode when calling from again to the same function. Promise do not change to "resolve" mode when calling again to the same function. Nov 4, 2017
@roysG roysG changed the title Promise do not change to "resolve" mode when calling again to the same function. Promise do not change to "resolve" mode when calling the function again. Nov 4, 2017
@constgen
Copy link

constgen commented Nov 6, 2017

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
Copy link
Author

roysG commented Nov 7, 2017

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
Copy link
Author

roysG commented Nov 8, 2017

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.

@roysG roysG closed this as completed Nov 8, 2017
@constgen
Copy link

constgen commented Nov 8, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants