简体   繁体   中英

Multiple functions with JavaScript promises

Goal:

Using JavaScript promises, on load(), if the database is not already populated, seed it with data, then display the data with getWidgets(), otherwise, if the db is already seeded just getWidgets().

The code:

const initialWidgets = [
  {id: 1, color: 'Red', sprocketCount: 7, owner: 'John'},
  {id: 2, color: 'Taupe', sprocketCount: 1, owner: 'George'},
  {id: 3, color: 'Green', sprocketCount: 8, owner: 'Ringo'},
  {id: 4, color: 'Blue', sprocketCount: 2, owner: 'Paul'}
];

require('./model');
const Widget = require('mongoose').model('WidgetModel');

function seedWidgets() {
  let results = [];
  Widget.find({}, function (err, collection) {
    if (err) { throw err;}

    if (collection.length === 0) {
      initialWidgets.map(widget => {
        Widget.create(widget);
      });
    }
  })
}

export function getWidgets(req) {
  let widgets = req.session.widgets;
  if (!widgets) {
    /// ?? seed database  
   /// ?? add new records to session
  }
  return widgets;
}

export default function load(req) {
  return new Promise((resolve, reject) => {
    resolve(getWidgets(req));
  })
}

What would be the proper way to handle these three methods with promises when in load() ...

  1. seed database if not already populated
  2. add the records to session
  3. display session objects

Thanks

FYI: This is an enhancement to the project:
https://github.com/erikras/react-redux-universal-hot-example

Promises are new to me. This works but I do not know if it is the correct approach.

I would greatly appreciate those more knowledgeable then I to speak out about my solution, ie good or bad or if there is something better.

const initialWidgets = [
  {id: 1, color: 'Red', sprocketCount: 7, owner: 'John'},
  {id: 2, color: 'Taupe', sprocketCount: 1, owner: 'George'},
  {id: 3, color: 'Green', sprocketCount: 8, owner: 'Ringo'},
  {id: 4, color: 'Blue', sprocketCount: 2, owner: 'Paul'}
];

require('./model');
const Widget = require('mongoose').model('WidgetModel');

function seedWidgets() {
  let results = [];
  return new Promise((resolve, reject) => {
    Widget.find({}, (err, collection) => {
      if (collection.length === 0) {
        initialWidgets.map(widget => {
          Widget.create(widget);
          results.push(widget);
        });
      }
      if (err) reject(err);
      else resolve(results);
    })
  });
}

export default function load() {
  return new Promise((resolve, reject) => {
        resolve(getWidgets(req));
  });
}
export default function getWidgets() {
  return new Promise((resolve, reject) => {
    seedWidgets().then((result) => {
      Widget.find({}).then((result) => {
        resolve(result);
      })
    }).catch((err) => {
      reject(err);
    })
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM