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?
知道我做错了什么吗?
3 个解决方案
#1
12
Q promises can be resolve
d 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()
.
Q promise可以只用一个参数来解决 - 一个promise表示一个值,而不是它们的集合。如果需要多个值,请将它们显式放入数组中。对于多参数回调,您可以使用.spread()。
#2
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.
创建使用许多值解析的异步函数是一种不好的做法。如果要传递多个值,请在数组或dict对象中返回它们,就像给定函数是同步的一样。
#3
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.
如果要传递多个值,则必须将它们包装在另一个传递的值中,例如数组或对象。
#1
12
Q promises can be resolve
d 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()
.
Q promise可以只用一个参数来解决 - 一个promise表示一个值,而不是它们的集合。如果需要多个值,请将它们显式放入数组中。对于多参数回调,您可以使用.spread()。
#2
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.
创建使用许多值解析的异步函数是一种不好的做法。如果要传递多个值,请在数组或dict对象中返回它们,就像给定函数是同步的一样。
#3
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.
如果要传递多个值,则必须将它们包装在另一个传递的值中,例如数组或对象。