Promise is a very good Lightweight Framework for JavaScript to implement elegant programming. This framework can free you from the messy multi-asynchronous callback code and focus on your business logic. 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
* 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 (function (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. _ isdone = true; for (var I = 0; I
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 );});