Rod
Rod

Reputation: 15455

Why do I need a promise when Sequelize query is a promise?

If Contracts.findAll is a promise, why do I need to put a promise around that? or do I need a promise in this interaction between these 2 files? (Note: I do want separate files, but do I need promise and the async/await)?

app.js

(async () => {
    var results2 = await contracts.get();
    console.log(results2);
})();

service.js

exports.get = function () {
    return new Promise(function (resolve, reject) {
        Contract.findAll().then(contracts => {
            resolve(contracts[0].AccountNo_Lender)
        });
    });
};

Upvotes: 1

Views: 612

Answers (1)

Tsvetan Ganev
Tsvetan Ganev

Reputation: 8856

You don't need a wrapper Promise if Contract.findAll() already returns a promise.

The following code is equivalent:

exports.get = function () {
  return Contract.findAll().then(
    contracts => contracts[0].AccountNo_Lender
  );
};

Upvotes: 3

Related Questions