Use Q.js to implement promise of API in Node.js _node.js

Source: Internet
Author: User
Tags readfile

about what is promise and promise solve the problem, please experience node callback asynchronous coding Dafa, incidentally Http://wiki.commonjs.org/wiki/Promises/A to see how it is defined, no longer repeat.

Here we look at how to implement the node API promise with Q.js.

First, everything is install

Copy Code code as follows:

NPM Install Q

Second, the standard node style API Promise method

1. Use Q.nfcall

Relative to Q.fcall, Q.nfcall is node's q.fcall.

Copy Code code as follows:

var FS = require (' FS '),
Q = require (' Q '),
Colors = require (' colors '),
File = ' Example.txt ';
var fsreadfile = Q.nfcall (fs.readfile,file,encoding);
Fsreadfile.then (function (result) {
Console.log (("Invoke in Nfcall" + file). Red);
Console.log (Result.green);
},function (Error) {
Console.log ("Invoke in Nfcall". Red);
Console.log (Error.tostring (). Red);
}
);

2. Use Q.denodeify

Copy Code code as follows:

var fsreadfile_denodeify = q.denodeify (fs.readfile);

Fsreadfile_denodeify (file,encoding). Then (function (Result) {
Console.log ("Invoke in Denodeify". Red);
Console.log (Result.green)
},function (Error) {
Console.log ("Invoke in Denodeify". Red);
Console.log (Error.tostring (). Red);
}
);

3. Use Q.deferd

Copy Code code as follows:

var fsreadfile_deferd = function (file,encoding) {
var deferred = Q.defer ();
Fs.readfile (File,encoding,function (error,result) {
if (Error) {
Deferred.reject (Error.tostring (). Red);
}
Deferred.resolve (result);
});

return deferred.promise;
};

Fsreadfile_deferd (file). Then (function (Result) {
Console.log ("Invoke in Deferd". Red);
Console.log (Result.tostring (). Green);
},function (Error) {
Console.log ("Invoke in Deferd". Red);
Console.log (Error.tostring (). Red);
}
);

4. Use Makenoderesolver ()

Copy Code code as follows:

var fsreadfile_makenoderesolver = function (file,encoding) {
var deferred = Q.defer ();
Fs.readfile (File,encoding,deferred.makenoderesolver ());
return deferred.promise;
};

Fsreadfile_makenoderesolver (file,encoding). Then (function (Result) {
Console.log ("Invoke in Makenoderesolver". Red);
Console.log (Result.green);
},function (Error) {
Console.log (Error.tostring (). Red);
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.