The Problem of promise Library in nodejs

  node.js, question
var promise =new Promise(function (resolve,reject) {
 return Promise.resolve(mongo.merchant.findOne({id: merchantId}));
 });
 promise.then((merchant)=> {
 return Promise.resolve(mongo.display.find({id: {'$in': merchant.displays}}));
 }).then((displays)=> {
 return Promise.resolve(displays);
 }).catch((err)=>{
 return Promise.reject(err);
 });
 bracket

This returns the value to

displayService.getByMerchantId(req.query.merchantId).then((displays)=>

This is a mistake.
clipboard.png
Daniel asked for help, feeling that I had a place to completely understand the mistake, but could not find a place.

Innew Promise()Inside should notreturn Promise.resolve()Well, it should be directreturn resolve()That’s right.

var promise = new Promise(function(resolve, reject) {
 // return Promise.resolve("hello");
 resolve("hello");
 });

But this is not the key to the problem, the key depends on youdisplayService.getByMerchantId()What was returned? You have to return onePromiseObject talentthenAh.

If what you gave above isgetByMerchantId()The content of the, probably should change like this

var displayService = {
 getByMerchantId: function() {
 var promise = new Promise(function(resolve, reject) {
 resolve(mongo.merchant.findOne({
 id: merchantId
 }));
 });
 
 return promise.then((merchant) => {
 return Promise.resolve(mongo.display.find({
 id: {
 '$in': merchant.displays
 bracket
 }));
 }).then((displays) => {
 return Promise.resolve(displays);
 }).catch((err) => {
 return Promise.reject(err);
 });
 bracket
 bracket

But the last one in the functionthenAndcatchThey are redundant and can be simplified.

var displayService = {
 getByMerchantId: function() {
 var promise = new Promise(function(resolve, reject) {
 resolve(mongo.merchant.findOne({
 id: merchantId
 }));
 });
 
 return promise.then((merchant) => {
 return Promise.resolve(mongo.display.find({
 id: {
 '$in': merchant.displays
 bracket
 }));
 });
 bracket
 bracket