这段代码中传播函数的目的是什么?

What is the purpose of spread function in this code?

我正在尝试弄清楚如何使用 mysql-promise。 https://www.npmjs.com/package/mysql-promise

这是一些示例代码;

var db = require('mysql-promise')();

db.configure({
    "host": "localhost",
    "user": "foo",
    "password": "bar",
    "database": "db"
});

db.query('UPDATE foo SET key = ?', ['value']).then(function () {
    return db.query('SELECT * FROM foo');
}).spread(function (rows) { //what's purpose of spread()?
    console.log('Loook at all the foo', rows);
});

传播函数的目的是什么?它具体有什么作用?

spread函数来自mysql-promise库自带的Bluebird promises库。它打开一个必须 return 数组的承诺,并将该数组的每个元素提供给传递给它的函数,在本例中是来自 mysql 数据库的行。

来自 mysql-promise 在 github 上的 package.json:

 "dependencies": {
    "bluebird": "^2.10.2",
    "mysql": "^2.10.2"
  },

以下是 bluebird 项目的更多信息: http://bluebirdjs.com/docs/api/spread.html

.spread 是一个 Bluebird(`mysql-promise 使用的 promise 库)函数。

基本上,.spread 允许您处理 promise 中的 return 值,该值是一个数组而不是单个值。

有关详细信息,请参阅 http://bluebirdjs.com/docs/api/spread.html

Jaromanda X 是正确的,示例有误spread

传播的理想用例是当您的回调/链接函数需要多个参数,但承诺 returns 单个值(我们的工作是确保它是链接函数使用的格式的参数数组), 所以:

Promise.resolve([1,2,3]).spread(function(a, b, c){  ...

等同于 ( 在 ES6 中):

Promise.resolve([1,2,3]).then( ([a, b, c]) => {  ...