JavaScript承诺不传递所有参数(使用Q)

我无法通过所有参数。 我的承诺callback只接收一个而不是三个:

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); }); 

任何想法我做错了什么?

Q承诺只有一个论点才能resolve – 一个承诺代表一个单一的价值,而不是一个集合。 如果您需要多个值,请将它们明确放入数组中。 对于多参数callback,可以使用.spread()

同步函数只返回一个值,asynchronous应该用同一个方法parsing。

创build使用多个值parsing的asynchronous函数是一种不好的做法。 如果你想传递许多值,返回他们在数组或字典中的对象,就像你会做的一样,如果给定的function将是同步的。

如果要传递多个值,则必须将它们包装在另一个传递的值中,例如数组或对象。