2

How do I bind the resolved return value (in this case a json object) of a Javascript promise in a let block? all I get is a #object[Promise [object Promise]]

For instance:

(.then (fn-that-returns-a-js-promise) #(print (.-prop %))) 

prints the value of prop to the the console, while:

(let [prop (.then (fn-that-returns-a-js-promise) #(.-prop %))] (print prop))

prints #object[Promise [object Promise]]

1 Answer 1

1

You can't bind it.

(.then (fn-that-returns-a-js-promise) #(.-prop %))

returns javascript Promise. Every operation on a promise returns the new promise, and your program logic executes within the context of that promise, you can't escape it.

Sign up to request clarification or add additional context in comments.

6 Comments

But calling print apparently forces it?
@mac .then(fn)'s return value is always a Promise, regardless of what function you've passed to it. That's a "limitation" of JavaScript, not ClojureScript.
In your first expression , print is called inside the promise, and it will be executed (printed) once the original promise is released. Your second expression just prints the promise, not its value. The value can be accessed just ffrom the promise itself
Thanks for the comments. Why is it that the property access inside the second callback does not return the value instead of the promise? it is afterall executed "inside" the promise.
It is returning the value, but "wrapped" inside the promise. If you want to read the value, you need to access it from the promise: (.then (.then (fn-that-returns-a-js-promise) #(.-prop %)) (fn[x](println x))) .There is no way for a value inside the promise to escape outside it, you have to chain the promises to operate on them(every call to then returns the new promise)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.