Implement the promises library of nodejs Based on promise. js, promise. jsnodejs
Today, I downloaded promise. js from the GIT source code library and found that the source code is written based on Web Front-end JavaScript and cannot be directly used for nodejs. Fortunately, there are not many codes and they are not very complicated. After analysis and integration, it is implemented as a nodejs framework. The Code is as follows:
(Function () {/*** Copyright 2012-2013 (c) Pierre Duquesne <stackp@online.fr> * script: promise. js * description: promises nodejs module * modified: https://github.com/stackp/promisejs* authors: alwu007@sina.cn **/var Promise = exports. promise = function () {this. _ callbacks = [] ;}; Promise. prototype. then = function (func, context) {// function doCallbackResults (r) {if (r instanceof Promise) {r. then (f Unction (err, values) {p. done (err, values) ;});} else {p. done (null, r) ;}} var p = new Promise (); if (this. _ isdone) {var results = func. apply (context, this. results); doCallbackResults (results);} else {this. _ callbacks. push (function () {var results = func. apply (context, arguments); doCallbackResults (results) ;}) ;}return p ;}; Promise. prototype. done = function () {this. results = arguments; this. _ I Sdone = true; for (var I = 0; I <this. _ callbacks. length; I ++) {this. _ callbacks [I]. apply (null, arguments);} this. _ callbacks = [] ;}; Promise. join = function (promises) {var p = new Promise (); var results = []; if (! Promises |! Promises. length) {p. done (results); return p;} var numdone = 0; var total = promises. length; function notifier (I) {return function () {numdone + = 1; results [I] = Array. prototype. slice. call (arguments); if (numdone === total) {p. done (results) ;};}for (var I = 0; I <total; I ++) {promises [I]. then (notifier (I);} return p ;}; Promise. chain = function (funcs, args) {var p = new Promise (); if (! Funcs |! Funcs. length) {p. done. apply (p, args);} else {funcs [0]. apply (null, args ). then (function () {funcs. splice (0, 1); Promise. chain (funcs, arguments ). then (function () {p. done. apply (p, arguments) ;}) ;}return p ;};})();
The additional test code is as follows:
/*** Script: test. js * description: promise. js test code **/var promise = require ('. /mypromise '); function asyncfoo () {var p = new promise. promise (); setTimeout (function () {p. done () ;}, 1000); return p;} function syncfoo () {var p = new promise. promise (); p. done (); return p;} var o ={};/* asyncfoo (). then (function () {return 'raymond ';}, o ). then (function (err, name) {o. name = name; return asyncfoo (). then (async Foo ). then (function () {return asyncfoo (). then (asyncfoo ). then (function () {return 18 ;}) ;}, o ). then (function (err, age) {o. age = age; return asyncfoo (). then (asyncfoo ). then (function () {return asyncfoo (). then (asyncfoo ). then (function () {return 'boys ';});}). then (function (err, sex) {return sex ;});}). then (function (err, sex) {o. sex = sex; return 'hello, world! ';}). Then (function (err, say) {o. say = say; console. dir (o) ;}); syncfoo (). then (function () {return 'raymond ';}, o ). then (function (err, name) {o. name = name; return syncfoo (). then (syncfoo ). then (function () {return syncfoo (). then (syncfoo ). then (function () {return 18 ;}) ;}, o ). then (function (err, age) {o. age = age; return asyncfoo (). then (asyncfoo ). then (function () {return asyncfoo (). then (asyncfoo ). then (fun Ction () {return 'boys ';});}). then (function (err, sex) {return sex ;});}). then (function (err, sex) {o. sex = sex; return 'hello, world! ';}). Then (function (err, say) {o. say = say; console. dir (o) ;}); */function asyncfoo1 () {var p = new promise. promise (); setTimeout (function () {p. done (null, 'raymond ') ;}, 1000); return p;} function asyncfoo2 (err, name) {o. name = name; var p = new promise. promise (); setTimeout (function () {p. done (null, 18) ;}, 1000); return p ;}function asyncfoo3 (err, age) {o. age = age; var p = new promise. promise (); setTi Meout (function () {p. done (null, 'Boy ') ;}, 1000); return p ;}function asyncfoo4 () {var p = new promise. promise (); setTimeout (function () {p. done (null, 'Hello, world! ');}, 1000); return p;} promise. promise. chain ([asyncfoo1, asyncfoo2, asyncfoo3]). then (function (err, sex) {o. sex = sex; return asyncfoo4 ();}). then (function (err, say) {o. say = say ;}). then (function () {console. dir (o );});
What is the relationship between JSON, JS, and NODEJS?
They are completely different in nature.
JavaScript is a JavaScript language and an explanatory programming language.
JSON is JavaScript Object Notation, which indicates the expression of objects in JavaScript language. It is often used for data transmission (similar to XML) and often replaces XML in AJAX.
NodeJS is a server platform that can run server scripts written in JavaScript on the server.
Note: In JS, functions are itself an object. Therefore, functions can be used as parameters not unique to NodeJS. To be precise, NodeJS uses Google V8 interpreter of Chrome browser to explain JS.
To sum up:
JS is a programming language.
JSON is a data format (no logic, only data)
NodeJS is a software (JS server running environment)
By the way, HTML is derived from XML (HTML is an XML), but changes to adapt to its specific role. HTML (XML) and JSON are the same data expression language, strictly speaking, the logic does not include only data.
What servers and databases are used for background websites using nodejs?
Nodejs is generally used for high-concurrency background programs, single-thread event-driven, V8 engine is awesome. If you play it yourself, at least use mysql or mysql. If it is a large project, it is oracle, I personally use mysql on the local machine, and the effect is OK.