如何在promise链中传递参数

我很担心在承诺之间传递价值,我不知道我能否在这里得到帮助。 我有一个CustomerController在CustomerRepo中调用方法

let getCustomers = customerRepo.getCustomers(req.query.offset, req.query.return); let getProfiles = customerRepo.getProfiles(rows); getCustomers .then(function (rows) { console.log(rows[0]); }) .then(getProfiles) 

我不知道如何将行(客户数组)传递给getProfiles,以便可以使用configuration文件填充客户对象。 这是CustomerRepo中的代码 –

 var getCustomers = function (offset, _return) { return new Promise(function (resolve, reject) { var sqlString = 'call qsel_customers' + '(' + offset + ',' + _return + ')'; pool.getConnection(function (err, connection) { if (err) { return reject(err); } connection.query(sqlString, function (err, rows) { if (err) { reject(err); } else resolve(rows); connection.release(); }); connection.on('error', function (err) { res.json({"code": 100, "status": "Error in connection database"}); }); }); }); } 

和getProfiles

  var getProfiles = function (rows) { return new Promise(function (resolve, reject) { console.log(rows); } } 

我得到行未定义。 也请有人build议如何提取db连接代码到dbConnect.js,以避免代码重复。

我不是承诺的专家,但我不认为把你的getCustomers承诺放在pool.getConnection是个好主意。 它看起来像另一个承诺,应该被链接而不是合并。

回到你的主要版本。 你可以这样做

 getCustomers .then(function (rows) { return getProfiles(rows); }) .then(function() { console.log('get profiles resolved'); }); 

  var getProfiles = function (rows) { return new Promise(function (resolve, reject) { resolve(rows); console.log('other log of rows:' + rows); } } 

编辑:

 let getCustomers = customerRepo.getCustomers(req.query.offset, req.query.return); let getProfiles = customerRepo.getProfiles; getCustomers .then(function (rows) { return getProfiles(rows); }) .then(function(rows) { console.log('rows number ' + rows); console.log('get profiles resolved'); ); 

Promise表示在某个时间点可能失败的计算。 因此,为了创造一个承诺你做:

 return new Promise(function(resolve, reject) { // here you decide when the computation fails and when succeeds resolve(1) // success reject('error') // failed }) 

诺言有一个方法(瑞士刀,的确如此)。 它采用了一个参数的函数。 现在,ƒ的魔力看起来像这样:

  • 如果ƒ返回简单的值(string,数字,数组等),然后将它包装在一个新的承诺,并返回它。 签名是then : Promise[a] -> (a -> b) -> Promise[b] 。 因此,假设你有一个函数∂返回一个数的Promise,ƒ取一个数并加上“!” 给你,并得到这个号码, then传递给ƒ使用, then结束一个新的承诺:

    ƒ.then(n => n +'!')// Promise [String]

  • 如果ƒ返回一个Promise,那么如果这个Promise将会“提取”这个值,把它传递给∂并返回一个新的Promise。 在伪代码中:

    ƒ.then(n => Promise(n +'!'))// Promise [String]

好的, then返回一个Promise。 好吧,我们来devise代码:

 let getCustomers = () => Promise.resolve([ {}, {}, {} ]) // assume myApi.getProfile takes a Customer and returns a Profile let getProfiles = (customers) => customers.map(myApi.getProfile) getCustomers().then(getProfiles).then(console.log.bind(console)); 

在链接Promise时,必须返回一个Promise,并使用从上游函数传递的值parsing,以便在下游函数中使用它。

 getCustomers .then(function (rows) { console.log(rows[0]); return Promise.resolve(rows[0]); }) .then(function(firstRow) { console.log("The first row's id is " + firstRow.id); return Promise.resolve(firstRow); }) .then(getProfiles); 

如果需要在其中一个函数中执行asynchronous工作,则返回一个新的Promise,并在asynchronouscallback中适当地调用resolve / reject函数。 Promise.resolve()是一个你可以使用的快捷方式,如果你没有asynchronous执行任何操作; 它只是简单地返回一个Promise对象,并用它传递的值来parsing。