简体   繁体   English

将参数传递给promise链

[英]Passing parameters to a promise chain

I'm having some problems with passing parameters through a promise chain I set up. 我在通过我设置的承诺链传递参数时遇到了一些问题。 Here's a basic example of what I'm trying to do. 这是我正在尝试做的一个基本的例子。

var dummyReq = { client: null };
var dummyUser = { email: 'user@secretdomain.com', password: 'admin' };

function printEmail(request, user) {
    return new Promise((resolve, reject) => {
        console.log('Email:', user.email);
        return resolve(request, user);
    });
}

function printPassword(request, user) {
    return new Promise((resolve, reject) => {
        console.log('Password:', user.password); // <-- user is undefined
        return resolve(request, user);
    });
}

printEmail(dummyReq, dummyUser)
    .then(printPassword)
    .catch(function(error) {
    console.log('Unexepected error has occured');
});

In the second promise the parameter user is undefined, how can I pass multiple parameters through a promise chain, as these promises continue more information gets added to the chain, by the end of it I'm passing 5 values through to the last promise. 在第二个promise中,参数user是未定义的,我如何通过promise链传递多个参数,因为这些promises继续将更多信息添加到链中,到最后我将5个值传递给最后一个promise。

Should I just use ES6 operators to combine them into an object and deconstruct them on every call? 我应该只使用ES6运算符将它们组合成一个对象并在每次调用时解构它们吗? for example return resolve({request, user}) and then const { request, user } = param 例如return resolve({request, user})然后是const { request, user } = param

You are on the right track. 你走在正确的轨道上。 Promise objects hold a single fulfilled value which is passed to onfulfilled reaction handlers. Promise对象包含一个已实现的值,该值将传递给onfulfilled反应处理程序。 A non promise value, or the fulfilled value of aa promise when it eventually becomes fulfilled, is used to fulfill the next promise in a chain of promises. 非承诺价值,或最终履行时承诺的履行价值,用于履行承诺链中的下一个承诺。 If you want to propagate multiple values to the next promise handler you have to use a single object value to do so. 如果要将多个值传播到下一个promise处理程序,则必须使用单个对象值来执行此操作。

So let's assume that printEmail and printPassword actually do something asynchronous rather than the synchronous operations in the post. 因此,我们假设printEmailprintPassword实际上是异步而不是帖子中的同步操作。

printEmail becomes: printEmail变为:

function printEmail(request, user) {
    return new Promise((resolve, reject) => {
        console.log('Email:', user.email);
        resolve( {request, user} );
    });
}

without a "return" statement inside the executor of the new Promise - the return value of an executor function is not used and will generally be undefined . 在新Promise的执行程序中没有“return”语句 - 不使用执行程序函数的返回值,并且通常是undefined

printPassword becomes printPassword成为

function printPassword(data) {
    return new Promise((resolve, reject) => {
        console.log('Password:', data.user.password);
        resolve(data);
    });
}}

Whether you deconstruct a data object into variables or not is a stylistic rather than technical question. 是否将数据对象解构为变量是一种风格而不是技术问题。 Similarly whether you pass the same object down to multiple handlers in a chain of promises, deleting or adding properties if required, or create a new object with only the properties needed in following steps is entirely a matter of choice and style. 类似地,无论是将同一个对象传递给Promise链中的多个处理程序,还是根据需要删除或添加属性,或者只创建一个只包含后续步骤所需属性的新对象,完全取决于选择和样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM