Promise.all(…).spread is not a function when running promises in parallel

左心房为你撑大大i 提交于 2019-12-05 00:03:05

I'll make my comment into an answer since it solved your issue.

.spread() is not a standard promise method. It is available in the Bluebird promise library. Your code you included does not show that. Three possible solutions:

Access array values directly

You can just use .then(results => {...}) and access the results as results[0] and results[1].

Include the Bluebird Promise library

You can include the Bluebird promise library so you have access to .spread().

var Promise = require('bluebird');

Use destructuring in the callback arguments

In the latest versions of nodejs, you could also use destructuring assignment which kind of removes the need for .spread() like this:

Promise.all([template,list]).then(function([templates,lists]) {
    res.render('campaign/create.ejs', {templates, lists});
});

You can write it without non-standard Bluebird features and keep less dependencies as well.

Promise.all([template,list])
  .then(function([templates,lists]) {
  };

ES6 Destructuring assignment

Promise.all([
  Promise.resolve(1),
  Promise.resolve(2),
]).then(([one, two]) => console.log(one, two));

This is a Bluebird Promise feature and you can access it via Sequelize.Promise without installing Bluebird module itself

Sequelize.Promise.all(promises).spread(...)

I needed to install BlueBird.

npm install bluebird

Then:

var Promise = require("bluebird");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!