javascript承诺不传递所有参数(使用Q) [英] javascript promise not passing all arguments (using Q)

查看:115
本文介绍了javascript承诺不传递所有参数(使用Q)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法传递所有论点。我的承诺回调只收到一个而不是三个:

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

知道我做错了吗?

推荐答案

Q promises可以只有一个参数 resolve d - 一个promise表示一个值,而不是它们的集合。如果需要多个值,请将它们显式放入数组中。对于多参数回调,您可以使用 .spread()

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().

这篇关于javascript承诺不传递所有参数(使用Q)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆