javascript-将参数传递给Promise.我必须打破连锁吗?

我是NodeJS和Promise的新手.我需要将参数传递到我的诺言链中的回调函数中.如下所示:

var first = function(something) {
/* do something */
return something.toString();
}
var second = function(something, item) {
/* need to work with both the args */
}

我的诺言链看起来像

function(item) {
    /* the contents come from first callback and the item should be passed as an argument to the second callback */
    fs.readFile(somefile).then(first).then(second)
}

我应该将项目作为参数传递,我可以在不中断链的情况下做到这一点吗?

如果我完全错了,请纠正我.

谢谢

解决方法:

将您的第二个函数包装为匿名函数,并通过以下方式传递参数:

function(item) {
    /* the contents come from first callback and the item should be passed as an argument to the second callback */
    fs.readFile(somefile)
        .then(first)
        .then(firstResult => second(firstResult, item))
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐