Node.js、Sails.js、Waterline和Bluebird中的.then和.spread是什么意思?

3

我正在尝试弄清楚sails中的承诺是如何工作的,已经成功地通过.then传递水线查询数据,但是无法利用.spread。我收到一个未定义的函数错误。有什么建议可以改进代码的第一部分以使其正常工作吗?

 //results in error
    Promise.all([Xyz.find(), Abc.find()]).spread(function (someOtherResult, yetAnotherResult) {
        console.log(someOtherResult)
    }).catch(function (err) {
        console.log(err);
    })

以下代码可以正常运行,但要从中提取数据可能会更加困难,或需要过长的嵌套.then语句:
    Promise.all([Xyz.find(), Abc.find()]).then(function (results) {
        console.log(results[0][1]);
        console.log(results[0].length);
    })


    Abc.find().then(function (foundAbcs) {
        Promise.all(Xyz.find().then(function (foundXyzs) {
            console.log(foundAbcs);
            console.log(foundXyzs);
            // additional syncranouse logic with Abc and Xyz
        }))
    })
1个回答

2

好的,非常简单的错误,我没有意识到需要:

var Promise = require('bluebird');

在sails.js .11中的module.exports之前,问题解决了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接