16

I am having trouble passing all arguments. My promise callback only receives one instead of three:

var asyncFunction= function(resolve) {
    setTimeout(function() {
        resolve("Some string that is passed", "and another", "third");
    }, 1000);
};

var promiseFunction = function () {
    var deferred = Q.defer();

    asyncFunction(deferred.resolve);

    return deferred.promise;
};

promiseFunction().then(function() {
    // Only one argument is passed here instead of 3
    // { '0': 'Some string that is passed' }
    console.log(arguments); 
});

Any idea what I am doing wrong?

1
  • my money is on type coercion Commented Jul 31, 2013 at 12:33

3 Answers 3

13

Q promises can be resolved with only one argument - a promise stands for one single value, not for a collection of them. Put them in an array explicitly if you need multiple values. For the multiple-parameter-callbacks, you can use .spread().

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

11 Comments

how is that possible? There are thousands of libraries with async functions out there that return multiple arguments? Can't they be used then?
Only with a wrapper it seems. Do you have a particular one in mind?
@Tomalak: I'd say there's a lot of value (my own promise implementation allows it) and passing around arguments objects can simplify the code. Of course, Q was built with a different paradigm from scratch and changing it would be complex now.
@Tomalak: You can write more concise callback functions, without accessing properties/indices on the value. Think of the arrays as tuples, not as lists. The $.ajax promises would be a good example.
@ForbesLindesay: Actually I don't see A+ forbidding multiple values - I guess we'll have to discuss that at some time, I'll open an issue. And even synchronous code can return multiple values - while currently not allowed in JavaScript (destructuring assignments might give a syntax for that) it is a feature in other languages.
|
2

Synchronous functions return only one value, same way asynchronous should resolve with one.

It's a bad practice to create async functions that resolve with many values. If you want to pass many values, return them in array or dict object, same as you would do if given function would be synchronous.

4 Comments

Plenty of libraries using callbacks pass back multiple arguments; I don't consider this bad practice. Promises/Futures supporting multiple values also seems like a logical step. I think this should be explored, as does this mailing list entry.
Matt, initially in library I maintain I experimented with allowance of multiple values for resolution, but I quickly rejected that idea. I don't remember now the exact issues, but I believe there were cases when I needed to deal with resolved value as one object, and it was weird (and not on par with sync functions) to always deal with an array or arguments object. Decision is already coined and proven to be right in practice. The post you've linked as you see was quickly put down. I don't remember such idea being seriously considered.
I only care because I'm writing a promises library (for fun and learning) and multiple resolving arguments feels like such a natural solution to me. If the community has settled on a single value, so be I suppose. Thanks for the follow-up.
@Matt, couldn't agree more. Promises solve the problem of nasty callback nesting yet callbacks still allow for more than one argument passed. It feels quite ridiculous that I can't pass more than one argument to .resolve, instead I have to pack it up into an array and unpack it in the callback leading to a mess. Wasn't that what promises was avoiding in the first place, a mess?
0

If you want to pass along multiple values, you must wrap them in another single value that you pass, such as an array or an object.

Comments

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.