Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- function Reader() {
- this.content = null;
- }
- Reader.prototype.getContent = function() {
- if (this.content === null) {
- return new Promise((resolve, reject) => {
- console.log('new promise');
- fs.readFile('file', 'utf8', function(err, contents) {
- if (err) return reject(err);
- this.content = contents;
- console.log('resolved content', this.content);
- resolve(this.content);
- });
- });
- }
- return Promise.resolve(this.content);
- }
- Reader.prototype.getContent2 = function() {
- if (this.content === null) {
- this.content = new Promise((resolve, reject) => {
- console.log('new promise');
- fs.readFile('file', 'utf8', function(err, contents) {
- if (err) return reject(err);
- this.content = contents;
- console.log('resolved content', this.content);
- resolve(this.content);
- });
- });
- }
- return this.content;
- }
- console.log('after calling readFile');
- var r = new Reader();
- Promise.all([r.getContent(), r.getContent()])
- .then((results) => {
- console.log(results);
- });
- Promise.all([r.getContent2(), r.getContent2()])
- .then((results) => {
- console.log(results);
- });
Add Comment
Please, Sign In to add comment