繁体   English   中英

如何在诺言链中传递参数

[英]how to pass parameters in promise chain

我真的很想在诺言之间传递价值,我想知道我是否可以在这里获得帮助。 我在CustomerRepo中有一个CustomerController调用方法

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,因此我可以用配置文件填充客户对象。 这是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);
  }
}

我得到未定义的行。 还请有人建议如何将db mysql连接代码提取到dbConnect.js中,以避免代码重复。

承诺表示在某个时间点可能会失败或不会失败的计算。 因此,为了创建一个Promise,您可以执行以下操作:

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

Promise有一个方法then (实际上是瑞士刀)。 它采用一个参数的函数ƒ。 现在,ƒ的魔力看起来像这样:

  • 如果ƒ返回简单值(字符串,数字,数组等),则它将包装在新的Promise中并返回它。 签名如下then : Promise[a] -> (a -> b) -> Promise[b] 因此,假设您有一个函数∂,它返回一个数字的承诺,而ƒ接收一个数字并加“!”。 为此,您将获得此编号, then使用then将其传递给ƒ,最后得到一个新的Promise:

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

  • 如果ƒ返回一个承诺,然后then将“提取物”的值,如果这个承诺,它传递给和∂与返回结果的新承诺。 用伪代码:

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

好的, then返回一个Promise。 好吧,让我们设计代码:

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

我不是Promises方面的专家,但我认为将pool.getConnection放入您的getCustomers承诺中不是好主意。 它看起来像另一个承诺,应该链接起来而不是结合起来。

回到您的主要问题。 你可以这样做

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

链接Promises时,必须返回一个由您从上游函数传递来的值解决的Promise,以便在下游函数中使用它。

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

如果您需要在其中一个函数中执行异步工作,请返回一个新的Promise并在异步回调中适当地调用resolve / reject函数。 Promise.resolve()是您不进行异步操作时可以使用的快捷方式。 它只是返回一个Promise对象,该对象用传递的值解析。

暂无
暂无

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

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