Aujourd'hui, j'ai téléchargé Promise.js à partir de la bibliothèque de code source GIT et j'ai constaté que le code source est rédigé en fonction de JavaScript Front-end et ne peut pas être utilisé directement pour NodeJS. Heureusement, le code n'est pas beaucoup, ni très compliqué. Après analyse et intégration, il est implémenté comme un cadre pour NodeJS, avec le code comme suit:
(Function () {/ *** Copyright 2012-2013 (c) Pierre Duquesne <[email protected]> * Script: promesse.js * Description: Promises Nodejs Module * Modified: https://github.com/stackp/promisejs* Authors: [email protected] * / var promesse = export.promise = furet () {this._cnack * / var promesse = export.promise = fure =) Var Results = Func.Apply (contexte, this.Results); for (var i = 0; i <this._callbacks.length; i ++) {this._callbacks [i] .Apply (null, arguments);} this._callbacks = [];}; promesse.join = function (promises) {var p = new promed (); var résultats = []; if (! promises ||! promises.length) {p.done (résultats); Retour p; } var numdone = 0; var total = promises.length; Notation de fonction (i) {return function () {numDone + = 1; résultats [i] = array.prototype.slice.call (arguments); if (numDone === Total) {P.Done (résultats); }}; } pour (var i = 0; i <total; i ++) {promises [i] .then (notifier (i)); } return p;}; promesse.chain = function (funcs, args) {var p = new promest (); if (! funcs ||! funcs.length) {p.done.apply (p, args); } else {funcs [0] .Apply (null, args) .then (function () {funcs.splice (0, 1); promesse.chain (funcs, arguments) .then (function () {p.done.apply (p, arguments);});}); } return p;};}) ();Le code de test est également joint comme suit:
/ *** script: test.js * Description: promesse.js Code de test * * / var promesse = require ('./ myPromes'); fonction asyncfoo () {var p = new promest.promise (); setTimeout (function () {p.done ();}, 1000); return p;} function syncoO () {var p = new promed.promise (); p.done (); return p;} var o = {}; / * asyncfoo (). puis (fonction () {return 'Raymond';}, o) .then (function (err, name) {o.name = name; return asyncfoo (). puis (asyncfoo) .then (function () {return asyncfoo (). puis (asyncfoo) .then (furet (). });});}, o) .then (fonction (err, age) {o.age = age; return asyncfoo (). alors (asyncfoo) .then (function () {return asyncfoo (). });}). puis (fonction (err, sexe) {o.sex = sexe; return 'Hello, world!';}). alors (fonction (err, say) {o.say = say; console.dir (o);}); syncoo (). puis (function () {return 'Raymond';}, o). syncfoo (). puis (syncoo) .then (function () {return syncoO (). alors (syncoo) .then (function () {return 18;});});}, o) .then (function (err, age) {o.age = gel; return asyncfoo (). puis (asyncfoo) .then (function () {return (return (return (). asyncfoo (). puis (asyncfoo) .then (function () {return 'boy';});}). alors (fonction (err, sexe) {return sex;});}). puis (fonction (err, sexe) {o.sex = sexe; return 'Hello, world!';}). puis (fonction (err, say) {o.say = say; console.dir (o);}); * / function asyncfoo1 () {var p = new promed.promise (); setTimeout (function () {p.done (null, 'Raymond');}, 1000); return p;} fonction asyncfoo2 (err, name) {o.name = name; var p = nouvelle promesse.promise (); setTimeout (function () {p.done (null, 18);}, 1000); return p;} fonction asyncfoo3 (err, age) {o.age = age; var p = nouvelle promesse.promise (); setTimeout (function () {p.done (null, 'boy');}, 1000); return p;} fonction asyncfoo4 () {var p = new promed.promise (); setTimeout (function () {p.done (null, 'bonjour, monde!');}, 1000); return p;} promesse.promise.chain ([asyncfoo1, asyncfoo2, asyncfoo3]). alors (fonction (err, sexe) {o.sex = sexe; return asyncfoo4 ();}). alors (fonction (err, say) {o.say = say;}). puis (fonction () {Console.dir (o);});