KobbyPemson
KobbyPemson

Reputation: 2539

Arity exception deref'ing promise

I'm using the http-kit library to make some webcalls and it returns a promise for each. When I try to deref any of the promises in the vector I get the following error

ArityException Wrong number of args (1) passed to: core/eval5473/fn--5474 clojure.lang.AFn.throwArity (AFn.ja va:429)

Simplest way to reproduce in a repl without http-kit is as follows

Create collection

(def x [ [1 (promise)] [2 (promise)] [3 (promise)]])

Simple Test

(map first x)
;user=>  (1 2 3)

My Test

(map #(vector % @%2) x)
;user=> ArityException Wrong number of args (1) passed to: user/eval109/fn--110  clojure.lang.AFn.throwArity (AFn.java
:429)

Update

I should probably delete this question. The problem had nothing to do with promises as Valentin noted below. I was typing %2 and thinking second argument. When what i needed was @(second %). i.e second entry in first and only argument.

Upvotes: 1

Views: 239

Answers (1)

Valentin Waeselynck
Valentin Waeselynck

Reputation: 6061

The function that is the second argument of map must accept only 1 argument in this case (which is meant to be an element of the seq that is being walked through).

You seem to be mistaking passing 2 arguments to a function and passing 1 argument that is a vector of 2 elements.

What you want to write is

(map (fn [[a b]] (vector a @b)) x)

...whereas what you're currently writing is equivalent to:

(map (fn [a b] (vector a @b)) x)

So this is not a problem about promises in fact.

Upvotes: 2

Related Questions