Promise is mainly used to solve the problem of asynchronous callback. There are several better solutions than promise. In this section, let's use promise to transform it. I wrote an article before. javascript Tencent Interview Question Learning Encapsulates a Simple Asynchronous Queue
An interview question (there is a button on the page, a ul, when you click the button, add a li to the back of UL every 1 second, a total of 10, the content of li starts from 0 (0, 1, 2,.... 9).
A small example of promise:
1 function next1(){ 2 return new Promise( function( resolve, reject ){ 3 console.log( 'next1' ); 4 resolve( 'jump to next2' ); 5 } ); 6 } 7 8 function next2(){ 9 return new Promise( function( resolve, reject ){ 10 console.log( 'next2' ); 11 resolve( 'jump to next3' ); 12 } ); 13 } 14 15 next1().then( function( res ){ 16 console.log( res ); 17 return next2(); 18 } ).then( function( res ){ 19 console.log( res ); 20 } );
Improvement scheme using promise:
1 <input type="button" value="Add to"> 2 <ul></ul> 3 <script> 4 var oUl = document.querySelector("ul"); 5 var oBtn = document.querySelector("input"); 6 var count = 0; 7 var timer = null; 8 function createDom() { 9 var oLi = document.createElement("li"); 10 oLi.innerHTML = count++; 11 oUl.appendChild(oLi); 12 } 13 var addDom = (function () { 14 return new Promise(function (resolve, reject) { 15 resolve(createDom); 16 }); 17 })(); 18 oBtn.onclick = function () { 19 timer = setInterval(function () { 20 if (count == 10) { 21 clearInterval(timer); 22 } else { 23 addDom.then(function (fn) { 24 fn(); 25 }); 26 } 27 }, 1000); 28 } 29 </script>
Next, we use promise to implement a simple todolist (message board) by combining Node.js+jade+mongodb+mongoose+express.
Project structure:
The results achieved are as follows:
public: Static resources for stored projects, such as images, css,js, etc.
routers: Separate different functional modules from different routes, such as index. JS (Home Routing), add. JS (Routing When Releasing Messages)
views: jade template directory
db.js: Database Connection Module
server.js: Start Server and Project Entry
server.js:
1 var express = require('express'); 2 var app = express(); 3 var path = require( 'path' ); 4 var indexRouter = require( './routers/index.js' ); 5 var addRouter = require( './routers/add.js' ); 6 7 app.use( '/', indexRouter ); 8 app.use( '/add', addRouter ); 9 app.use( express.static( path.join( __dirname, 'public' ) ) ); 10 11 app.set('views', path.join(__dirname, 'views')); 12 app.set('view engine', 'jade'); 13 14 app.listen( 3000 ); 15 console.log( 'server listening on:' + 3000 );
index.js, render the home page, take out all the message data in the database
1 var express = require('express'); 2 var router = express.Router(); 3 var Message = require('../db'); 4 5 function getAllMsg(){ 6 return new Promise( function( resolve, reject ){ 7 Message.find( {}, function( err, msgList ){ 8 resolve( msgList ); 9 } ); 10 } ); 11 } 12 router.get('/', function (req, res) { 13 getAllMsg().then( function( data ){ 14 res.render( 'index', { 15 msgList : data 16 } ); 17 } ); 18 }); 19 module.exports = router;
add.js, Handling the Promotion Procedure for Publishing Messages
1 var express = require( 'express' ); 2 var router = express.Router(); 3 var Message = require( '../db' ); 4 5 function addMsg( msgInfo ){ 6 var msgModel = new Message( msgInfo ); 7 msgModel.save(function(err,result){ 8 if( err ){ 9 console.log( err ); 10 }else{ 11 console.log( 'success' ); 12 } 13 }); 14 } 15 router.get('/', function( req, res ) { 16 addMsg( { 17 title : req.query.msg, 18 listTime : new Date() 19 } ); 20 res.redirect( '/' ); 21 }); 22 23 module.exports = router;
db.js: Database Connection Module
1 var mongoose = require('mongoose'); 2 mongoose.Promise = global.Promise; 3 var DB_URL = 'mongodb://localhost:27017/cms'; 4 5 var db = mongoose.createConnection(DB_URL); 6 db.on('connected', function (err) { 7 if (err) { 8 console.log(err); 9 } else { 10 console.log('db connected success'); 11 } 12 }); 13 var Schema = mongoose.Schema; 14 var msgSchema = new Schema({ 15 title: String, //Message message 16 listTime: Date //Release time 17 }); 18 var Message = db.model( 'Message', msgSchema ); 19 20 module.exports = Message;
Follow-up functions:
1, delete
2, editors
3, paging
... and so on.